function | since v0.0-927 | clojure.core/peek | Edit |
(peek coll)
Returns the first element of a list; same as first
.
Returns the last element of a vector, and much more efficient than using last
.
Returns nil if coll
is empty.
With vectors:
(peek [1 2 3])
;;=> 3
(peek [1 2])
;;=> 2
(peek [1])
;;=> 1
(peek [])
;;=> nil
With lists:
(peek '(1 2 3))
;;=> 1
(peek '(1 2))
;;=> 1
(peek '(1))
;;=> 1
(peek '())
;;=> nil
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.
(defn peek
[coll]
(when-not (nil? coll)
(-peek coll)))