1.3.0 permalink Arrow_down_16x16
  • (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.

2 Examples top

  • user=> (def large-vec (vec (range 0 10000)))
    #'user/large-vec
    
    user=> (time (last large-vec))
    "Elapsed time: 1.279841 msecs"
    9999
    
    user=> (time (peek large-vec))
    "Elapsed time: 0.049238 msecs"
    9999
    
  • user=> (peek '(:a :b :c))
    :a
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1359 top

(defn peek
  "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."
  {:added "1.0"
   :static true}
  [coll] (. clojure.lang.RT (peek coll)))
Vars in clojure.core/peek:
Used in 0 other vars

Comments top

1 comment(s) for peek.

Small reminder:


Do not work for arbitrary seq but just for persistent types implementing clojure.lang.IPersistentStack (like clojure.lang.Persistent*).


Example:

user> (peek (cons 1 '()))
; Evaluation aborted.

do not work because type is clojure.lang.Cons but

user> (peek (conj '() 1))
1

works because type is clojure.lang.PersistentList.

Log in to add a comment.