You definitely want (lazy-seq (cons ...)) the default, deviating only if you have a clear reason for this. clojuredocs.org is fine, but the examples are all provided by the community, and I would not call them "docs." Of course, the consequence of how it is built is that examples are usually written by people who have just learned to use this design and want to help, so many of them are poor. Instead, I would refer to the code in clojure.core or other well-known code.
Why should this be the default? Consider these two map implementations:
(defn map1 [f coll] (when-let [s (seq coll)] (cons (f (first s)) (lazy-seq (map1 f (rest coll)))))) (defn map2 [f coll] (lazy-seq (when-let [s (seq coll)] (cons (f (first s)) (map2 f (rest coll))))))
If you call (map1 prn xs) , then the xs element will be implemented and printed immediately, even if you never intentionally implement the element of the resulting displayed sequence. map2 , on the other hand, immediately returns a lazy sequence, delaying all its work until an element is requested.
source share