(last coll)
Return the last item in coll, in linear time
user=> (last [1 2 3 4 5])
5
user=> (last ["a" "b" "c" "d" "e"])
"e"
user=> (last {:one 1 :two 2 :three 3})
[:three 3]
user=> (last [])
nil
;; but be careful with what you expect from a map (or set):
user=> (last {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9})
[:a 1]
;really slow reverse
;put the last item of the list at the start of a new list, and recur over all but the last item of the list.
;butlast acts similar to next in that it returns null for a 1-item list.
(defn my-reverse
([a-list]
(cond (= a-list nil) nil
:else (cons (last a-list)
(my-reverse (butlast a-list))))))
;; Prefer clojure.core/peek over `last` for potentially large vectors.
user=> (def v (into [] (take 900900 (range))))
#'user/v
user=> (time (last v))
"Elapsed time: 24.460958 msecs"
900899
user=> (def v2 (into [] (take 900900 (range))))
#'user/v2
user=> (time (peek v2))
"Elapsed time: 0.020498 msecs"
900899
;; For a deep dive into why Rich Hickey chose not to make `last` performant
;; for large vectors, please see:
;; https://gist.github.com/reborg/dc8b0c96c397a56668905e2767fd697f