store/[org.clojure/clojure "1.8.0"] clj::clojure.core/lazy-seq

Official Documentation

Add notes
Arities
==================================================
   [& body]

Docstring
==================================================
  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?

Source

(defmacro 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?"
  {:added "1.0"}
  [& body]
  (list 'new 'clojure.lang.LazySeq (list* '^{:once true} fn* [] body)))

Example 1

Edit
;; A lazy-seq of Fibonacci numbers (fn = fn-1 + fn-2)
;; The producer function takes exactly two parameters
;; (because we need the last 2 elements to produce a new one)
(defn fib [a b] (cons a (lazy-seq (fib b (+ b a)))))
;; => #'user/fib

(take 5 (fib 1 1))
;; => (1 1 2 3 5)

Example 2

Edit
;; An example combining lazy sequences with higher order functions
;; Generate prime numbers using trial division.
;; Note that the starting set of sieved numbers should be
;; the set of integers starting with 2 i.e., (iterate inc 2) 
(defn sieve [s]
  (cons (first s)
        (lazy-seq (sieve (filter #(not= 0 (mod % (first s)))
                                 (rest s))))))
;; => #'user/sieve

(take 20 (sieve (iterate inc 2)))
;; => (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71)

Example 3

Edit
;; It might be easier to think about the producer function as a function
;; that, given element n, produces element n+1 via a recursive call to 
;; itself, wrapped with lazy-seq to delay its execution
;; We might also provide no-argument version of the function that calls 
;; itself for the first element(s) of the sequence being generated.
;; => variant of fibonaci with a no-arg version and using cons first:
(defn sum-last-2 
  ([] (sum-last-2 1 2)) 
  ([n m] (cons n (lazy-seq (sum-last-2 m (+ n m))))))

(take 6 (sum-last-2))
;; => (1 2 3 5 8 13)

Example 4

Edit
;; The following defines a lazy-seq of all positive numbers.  Note that 
;; the lazy-seq allows us to make a recursive call in a safe way because
;; the call does not happen immediately but instead creates a closure.

(defn positive-numbers 
  ([] (positive-numbers 1))
  ([n] (cons n (lazy-seq (positive-numbers (inc n))))))
;; => #'user/positive-numbers

(take 5 (positive-numbers))
;; => (1 2 3 4 5)

Uses on crossclj