Last in Clojure
Chat GPT: No, the last
function is not particularly expensive for vectors
in Clojure. It runs in O(1) time because vectors in Clojure support efficient
access to their last element.
Meanwhile, Clojure:
Нашли ошибку? Выделите мышкой и нажмите Ctrl/⌘+Enter
Myke, 10th Oct 2024, link
Peek?
John, 11th Oct 2024, link
Well, docstring doesn’t lie :) But, you can always use (peek coll) or (first (rseq coll)) which are way faster:
=> (def x (into [] (range 1 10000)))
=> (time (last x)) “Elapsed time: 0.288122 msecs”
=> (time (peek x)) “Elapsed time: 0.026535 msecs”
=> (time (first (rseq x))) “Elapsed time: 0.040689 msecs”