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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to