A slightly modified version of reduce
was introduced with reducers , clojure.core.reducers/reduce
(short r/reduce
):
(defn reduce ([f coll] (reduce f (f) coll)) ([f init coll] (if (instance? java.util.Map coll) (clojure.core.protocols/kv-reduce coll f init) (clojure.core.protocols/coll-reduce coll f init))))
r/reduce
differs from its main brother only in that it uses (f)
as the initial value when none is provided, and it passes reduce-kv
to the kernel for the cards.
I don’t understand what might be the use of such an unusual special reduce
, and why it should be included in the reducer library.
Curiously, r/reduce
not mentioned in the two introductory blog posts, as far as I can tell (, second ). Official documentation notes
In general, most users will not directly access r / reduce and instead prefer r / fold (...) However, it may be useful to perform accelerated reduction with fewer intermediate results.
Not sure what the last sentence hints at.
In what situations can r/reduce
deal with what kernel reduction cannot? When can I achieve r/reduce
with conviction?
source share