Created
September 1, 2011 16:39
-
-
Save prajwalit/1186607 to your computer and use it in GitHub Desktop.
[4clojure #60] Write a function which behaves like reduce, but returns each intermediate value of the reduction. Your function must accept either two or three arguments, and the return sequence must be lazy.
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
(fn step-reduce | |
([f coll] | |
(step-reduce f (first coll) (rest coll))) | |
([f x coll] | |
(if (seq coll) | |
(let [next-coll (rest coll) | |
next-x (f x (first coll))] | |
(cons x (lazy-seq (step-reduce f next-x next-coll)))) | |
(cons x (lazy-seq '()))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment