Skip to content

Instantly share code, notes, and snippets.

@rxacevedo
Created April 4, 2014 20:22
Show Gist options
  • Save rxacevedo/92372b13eaa92a041437 to your computer and use it in GitHub Desktop.
Save rxacevedo/92372b13eaa92a041437 to your computer and use it in GitHub Desktop.
Lazy map implementation for use when doing side-effectual things by mapping some fn with side-effects to a seq.
(defn lazy-map [f coll]
(if (seq coll)
(lazy-seq (cons (f (first coll)) (lazy-map f (rest coll))))
nil))
;; The difference
clj-print.core> (type (seq (map print (range 50))))
012345678910111213141516171819202122232425262728293031
clojure.lang.ChunkedCons
clj-print.core> (type (seq (lazy-map print (range 50))))
0
clojure.lang.Cons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment