Skip to content

Instantly share code, notes, and snippets.

@fronx
Created September 8, 2011 18:56
Show Gist options
  • Select an option

  • Save fronx/1204303 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/1204303 to your computer and use it in GitHub Desktop.
;; Write a function which returns the last element in a sequence.
;: Special restrictions: last
(= (__ [1 2 3 4 5]) 5)
(= (__ '(5 4 3)) 3)
(= (__ ["b" "c" "d"]) "d")
(find-doc "last")
;; =>
; -------------------------
; ...
; clojure.core/last
; ([coll])
; Return the last item in coll, in linear time
; -------------------------
; ...
; -------------------------
; clojure.core/peek
; ([coll])
; For a list or queue, same as first, for a vector, same as, but much
; more efficient than, last. If the collection is empty, returns nil.
; -------------------------
(find-doc #"(?i)create.*vector")
(source last)
;; =>
(def
^{:arglists '([coll])
:doc "Return the last item in coll, in linear time"
:added "1.0"}
last (fn last [s]
(if (next s)
(recur (next s))
(first s))))
#(peek (vec %))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment