Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Last active December 24, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrtsa/6741499 to your computer and use it in GitHub Desktop.
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.
(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