You're viewing version 1.2.0 of reductions. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

reductions

clojure.core

  • (reductions f coll)
  • (reductions f init coll)
Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init.

1 Example top

  • 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
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5576 top

(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)))))))
Vars in clojure.core/reductions: cons defn first if-let lazy-seq list rest when-let
Used in 2 other vars reductions reductions

Comments top

No comments for reductions. Log in to add a comment.