I have a code (sentences
is iterator
here):
def count() = {
var count = 0
for(sentence <- sentences.toStream) count += sentence.words.size
count
}
and test:
// first
val wordCount1 = wordCounter.count()
wordCount1 must_== 10
// second time - should be same result
val wordCount2 = wordCounter.count()
wordCount2 must_== 10 // fails: result is 0
Last test fails:
'0' is not equal to '10'
Expected :10
Actual :0
But since I use sentences.toStream
in the code above, I suppose to have stream
at it is (I can reuse it, theoretically).
Q: why does it fail?
EDIT:
I was hoping that toStream
would help. Like was described here: (..."You can traverse the same Stream
multiple times"...). It's like I never touch iterator, I have deal with stream.
But I got.. sentences.toStream
used UP sentence-iterator
so I can not use it anymore. I just expected when doing toStream
on iterator
is do a logic like getting stream-'link' to iterator without touching iterator itself. Ok..