4 ms·
Reduce /IS/ a loop: (from clojure.core) ([f val coll] (let [s (seq coll)] (if s (if (chunked-seq? s) (recur f
by anonjon 17y ago
Reduce /IS/ a loop: (from clojure.core)
([f val coll]
(let [s (seq coll)]
(if s
(if (chunked-seq? s)
(recur f
(.reduce (chunk-first s) f val)
(chunk-next s))
(recur f (f val (first s)) (next s)))
val)))
It follows that I can use chunks in a loop... chunking and looping are not mutually exclusive.
It also happens to be the case that reduce is a function call, and (AFAIK) that you can't use transients (like conj!) or make use of unboxed values.
This is an issue, as such, one should still loop for best performance.
- swannodette 17y agoIf I'm not mistaken chunking is only supported on some of Clojure's data structures. Reduce is certainly the better option with vectors (and maps?) if you write your own loop/recur you'll simply be duplicating the existing implementation. Also I really can't imagine using loop/recur on anything except for vectors and maps most of the time anyway, so why not just use reduce?