clojure.core/lazy-seq¶
Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object that will invoke the body only the first time seq is called, and will cache the result and return it on all subsequent seq calls. See also - realized?
Examples¶
Note by hgijeon¶
I think every form of (cons a (lazy-seq (f b))) in examples should be changed to (lazy-seq (cons a (f b))) to be fully lazy.
Think about
(def one-over ((fn helper [n] (cons (/ 1 n) (lazy-seq (helper (inc n))))) 0))
and
(def one-over ((fn helper [n] (lazy-seq (cons (/ 1 n) (helper (inc n))))) 0))
First one throws an exception right after hitting enter key, while second code postpones the calculation(and that's the point of lazyness!).
Therefore, cons should be inside of lazy-seq.
See also¶
- clojure.core/lazy-cat (4)
- clojure.core/realized? (1)
- clojure.core/doall (0)
- clojure.core/iterate (0)
Content from the matching ClojureDocs page, with authors credited on each contribution.