You definitely want (lazy-seq (cons ...))
as your default, deviating only if you have a clear reason for it. clojuredocs.org
is fine, but the examples are all community-provided and I would not call them "the docs". Of course, a consequence of how it's built is that the examples tend to get written by people who just learned how to use the construct in question and want to help out, so many of them are poor. I would refer instead to the code in clojure.core, or other known-good code.
Why should this be the default? Consider these two implementations of map
:
(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 an element of xs will be realized and printed immediately, even if you never intentionally realize an element of the resulting mapped sequence. map2
, on the other hand, immediately returns a lazy sequence, delaying all its work until an element is requested.