Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init.
user=> (reductions + [1 1 1 1])
(1 2 3 4)
user=> (reductions + [1 2 3])
(1 3 6)
;; This is just like reduce except that the calculation is collected during the reduce.
user=> (assert (= (reduce + [1 2 3])
(last (reductions + [1 2 3]))))
nil
(defn reductions
"Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init."
{:added "1.2"}
([f coll]
(lazy-seq
(if-let [s (seq coll)]
(reductions f (first s) (rest s))
(list (f)))))
([f init coll]
(cons init
(lazy-seq
(when-let [s (seq coll)]
(reductions f (f init (first s)) (rest s)))))))
Comments top
No comments for reductions. Log in to add a comment.