Created
April 4, 2014 20:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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