What is the purpose of clojure.core.reducers/reduce?

Viewed 101

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 core sibling only in that it uses (f) as the initial value when none is provided, and it delegates to core reduce-kv for maps.

I don’t understand what use such an odd special-purpose reduce might be and why it was worth including in the reducers library.

Curiously, r/reduce is not mentioned in the two introductory blog posts as far as I can tell (first, second). The official documentation notes

In general most users will not call r/reduce directly and instead should prefer r/fold (...) However, it may be useful to execute an eager reduce with fewer intermediate results.

I’m unsure what that last sentence hints at.

What situations can r/reduce handle that the core reduces cannot? When would I reach for r/reduce with conviction?

1 Answers
Related