[CLJ-1218] mapcat is too eager Created: 16/Jun/13 Updated: 22/Apr/17
|Affects Version/s:||Release 1.5|
|Patch:||Code and Test|
The following expression prints 1234 and returns 1:
The reason is that (apply concat args) is not maximally lazy in its arguments, and indeed will realize the first four before returning the first item. This in turn is essentially unavoidable for a variadic concat.
This could either be fixed just in mapcat, or by adding a new function (to clojure.core?) that is a non-variadic equivalent to concat, and reimplementing mapcat with it:
|Comment by Gary Fredericks [ 17/Jun/13 7:54 AM ]|
I realized that concat could actually be made lazier without changing its semantics, if it had a single [& args] clause that was then implemented similarly to join above.
|Comment by John Jacobsen [ 27/Jul/13 8:08 AM ]|
I lost several hours understanding this issue last month [1, 2] before seeing this ticket in Jira today... +1.
|Comment by Gary Fredericks [ 05/Feb/14 1:35 PM ]|
Updated join code to be actually valid.
|Comment by Ghadi Shayban [ 21/May/15 8:32 PM ]|
The version of join in the description is not maximally lazy either, and will realize two of the underlying collections. Reason: destructuring the seq results in a call to 'nth' for 'x' and 'nthnext' for 'xs'. nthnext is not maximally lazy.