Consider the following Clojure code:
(let [s (range 10000000)]
(reduce + s))
This behaves nicely and returns the value 49999995000000
Now consider this code:
(loop [s (range 10000000)]
(reduce + s))
In this case, the loop binding holds on to the head of the sequence,
resulting in an OutOfMemoryError.
Of course, using loop without recur is pointless: this is only a
degenerate case to illustrate the problem. The issue actually does
show up in real code, where a loop might have a conditional to either
recur or bail to an alternative handler. This is actually happening
right now in the clojure.lang.Object impelementation of
clojure.core.protocols/internal-reduce.
Should I file this as a bug, or is there a good reason for this that I
haven't thought of?
Thanks,
-Luke