This behaviour is known and there are a couple of tickets about it :

https://dev.clojure.org/jira/browse/CLJ-1583
https://dev.clojure.org/jira/browse/CLJ-1218

On Wed, 18 Jul 2018, 08:28 Mark Engelberg, <[email protected]> wrote:

> I'm kind of surprised I haven't run across this before, but tonight I was
> debugging a function that was doing an explosion of computation to return
> the first value of a lazy sequence, and I was able to reduce the problem
> down to this example:
>
> > (first (apply concat (map #(do (println %) [%]) (list 1 2 3 4 5))))
> 1
> 2
> 3
> 4
> 1
>
> The last 1 is the return value, but notice that it realized 4 values in
> order to return the 1.  This has nothing to do with chunked sequences, by
> the way -- a list is an unchunked sequence.  It appears to be that the way
> concat is written, it realizes the first two elements, and then another two
> elements in a recursive call before the lazy-seq kicks in.
>
> In the function in question, the "apply concat" was part of a recursion,
> causing that explosion of realizing values (four at each level of the
> recursion, it would seem) to get at the first element.
>
> Note that this affects mapcat as well, which relies on concat under the
> hood:
> > (first (mapcat #(do (println %) [%]) (list 1 2 3 4 5)))
> 1
> 2
> 3
> 4
> 1
>
> I don't see a quick fix other than writing my own improved concat/mapcat.
> Do you?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to