take

added
1.0

ns
clojure.core

type
function

(take n) (take n coll)

Returns a lazy sequence of the first n items in coll, or all items if
there are fewer than n.  Returns a stateful transducer when
no collection is provided.

                ;; return a lazy seq of the first 3 items
(take 3 '(1 2 3 4 5 6))
;;=> (1 2 3)

(take 3 [1 2 3 4 5 6])
;;=> (1 2 3)

;; returns all items if there are fewer than n
(take 3 [1 2])
;;=> (1 2)

(take 1 [])
;;=> ()

(take 1 nil)
;;=> ()

(take 0 [1])
;;=> ()

(take -1 [1])
;;=> ()
            
                ;; similar to subvec but lazy and with seqs
(take 3 (drop 5 (range 1 11)))
;;=> (6 7 8)
            
                ;; Used without a collection, take will create a transducer:
(def xf (take 5))

;; We can now apply this transducer to a sequence:
(transduce xf conj (range 5))
;; => [0 1 2 3 4]