Skip to content

Instantly share code, notes, and snippets.

@prajwalit
Created September 1, 2011 16:39
Show Gist options
  • Select an option

  • Save prajwalit/1186607 to your computer and use it in GitHub Desktop.

Select an option

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.
(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