Reputation: 3056
Suppose you have this code:
(reduce + 1 (range 1 13000))
Should this not cause a stackoverflow, because it is not tail call optimized? Or is reduce similar to loop?
Upvotes: 0
Views: 125
Reputation: 70239
A reduce
will effectively be a loop over the values of the collection.
I say "effectively" because there are actually many different reduce implementation strategies employed by Clojure, depending on the type of the collection. This includes seq traversal via first/next, reduction via iterator, and collections that know how to self-reduce more efficiently.
In this case, the call to range
actually returns a LongRange
object which implements the IReduceInit
interface for self reduction and actually does a tight do/while
loop (implemented in Java).
The code for this can be found here in the latest release.
Upvotes: 6