Last active
December 24, 2015 04:09
-
-
Save pyrtsa/6741499 to your computer and use it in GitHub Desktop.
Alternative ways to achieve fully lazy (chunk size one) mapping over sequences in Clojure.
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 map1 | |
[f xs] | |
(reductions (fn [_ x] (f x)) nil xs)) | |
(defn map2 | |
[f xs] | |
(lazy-seq | |
(if (empty? xs) | |
nil | |
(cons (f (first xs)) | |
(map1 f (rest xs)))))) | |
(defn dechunk1 | |
[xs] | |
(reductions (fn [_ x] x) nil xs)) | |
(defn dechunk2 | |
[xs] | |
(lazy-seq | |
(if (empty? xs) | |
nil | |
(cons (first xs) | |
(dechunk2 (rest xs)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment