On 21.01.2009, at 20:33, Rich Hickey wrote:
> I've started documenting the streams work I have been doing, for those
> interested:
>
> http://clojure.org/streams
Nice!
I have played a bit with the stream implementation, and I came across
a behaviour that I do not understand:
First, define a random stream that calls rand, and an iter on it:
(def rand-stream (stream (fn [_] (rand))))
(def rand-iter (stream-iter rand-stream))
Calling it a few times shows that it works:
(next! rand-iter nil)
(next! rand-iter nil)
Next, try to use it as a seq:
(take 4 rand-stream)
This fails, as it should:
java.lang.IllegalStateException: Already iterating (NO_SOURCE_FILE:0)
Detach the iter and try again:
(detach! rand-iter)
(take 4 rand-stream)
Now it works - fine. But what happened to the seq that now owns the
stream? Nothing refers to it, so it should be gone. Did it perhaps
liberate the stream, so that I can create an iter again? Let's try:
(def rand-iter (stream-iter rand-stream))
(next! rand-iter nil)
(next! rand-iter nil)
It seems so. But... let's be mean:
(take 4 rand-stream)
I would expect this to throw the IllegalStateException again, but it
doesn't: it returns the same four-number sequence as the last time it
was called. Where was that one stored? In the stream itself? Or does
the stream keep a reference to the seq, so that it never disappears?
But then I shouldn't be able to create another iterator.
Let's be mean again:
(next! rand-iter nil)
(take 10 rand-stream)
(next! rand-iter nil)
(next! rand-iter nil)
(take 15 rand-stream)
All of these work - it seems I have both an iter and a seq on the
same stream, with the iter returning values that are also in the seq.
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---