lazy-cat

clojure.core

  • (lazy-cat & colls)
Expands to code which yields a lazy sequence of the concatenation
of the supplied colls. Each coll expr is not evaluated until it is
needed.

(lazy-cat xs ys zs) === (concat (lazy-seq xs) (lazy-seq ys) (lazy-seq zs))

3 Examples top

  • user=> (lazy-cat [1 2 3] [4 5 6])
    (1 2 3 4 5 6)
    
  • ;; N.B. this example holds onto the head of a lazy seq which should generally be avoided
    (def fib-seq
         (lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))
    
    (take 10 fib-seq)
  • ;; When the producer function produces a collection, not an element,
    ;; lazy-cat is usable.
    user=> (defn n-repeat [n] (lazy-cat (repeat n n) (n-repeat (inc n))))
    #'user/n-repeat
    
    user=> (take 6 (n-repeat 1))
    (1 2 2 3 3 3)
    
    user=> (take 12 (n-repeat 1))
    (1 2 2 3 3 3 4 4 4 4 5 5)
    
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:4064 top

(defmacro lazy-cat
  "Expands to code which yields a lazy sequence of the concatenation
  of the supplied colls.  Each coll expr is not evaluated until it is
  needed. 

  (lazy-cat xs ys zs) === (concat (lazy-seq xs) (lazy-seq ys) (lazy-seq zs))"
  {:added "1.0"}
  [& colls]
  `(concat ~@(map #(list `lazy-seq %) colls)))
Vars in clojure.core/lazy-cat:
Used in 0 other vars

Comments top

No comments for lazy-cat. Log in to add a comment.