A Lazy seq type.

lazy-cat

Available since version 1.0 (alias of clojure.core/lazy-cat)

MACRO (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) is the same as (seq (concat (lazy-seq xs) (lazy-seq ys) (lazy-seq zs)))

lazy-seq

Available since version 1.0 (alias of clojure.core/lazy-seq)

MACRO (lazy-seq & body)

Takes a body of expressions that returns an ISeq or nil, and yields a seq collection 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(defn number-seq-from
  [x]
  (lazy-seq
   (do (println! "processing number" x)
       (cons x (number-seq-from (inc x))))))
;;=> #'foo.bar/number-seq-from

(def natural-numbers (number-seq-from 0))
;;=> #'foo.bar/natural-numbers

(seq (take 10 natural-numbers))
;; processing number 0
;; processing number 1
;; processing number 2
;; processing number 3
;; processing number 4
;; processing number 5
;; processing number 6
;; processing number 7
;; processing number 8
;; processing number 9
;;=> (0 1 2 3 4 5 6 7 8 9)

;; when called again, no processing is done, as the results are cached
(seq (take 10 natural-numbers))
;;=> (0 1 2 3 4 5 6 7 8 9)

(seq (take 15 natural-numbers))
;; processing number 10
;; processing number 11
;; processing number 12
;; processing number 13
;; processing number 14
;;=> (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14)