【发布时间】:2013-06-04 23:05:28
【问题描述】:
我有一个代码(sentences 在这里是iterator):
def count() = {
var count = 0
for(sentence <- sentences.toStream) count += sentence.words.size
count
}
和测试:
// 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
上次测试失败:
'0' is not equal to '10'
Expected :10
Actual :0
但由于我在上面的代码中使用了sentences.toStream,我想stream 是(理论上我可以重用它)。
问:为什么会失败?
编辑:
我希望toStream 会有所帮助。正如here 所描述的那样:(...“您可以多次遍历相同的Stream”...)。就像我从不接触迭代器一样,我处理的是流。
但是我得到了.. sentences.toStream 用完了 sentence-iterator 所以我不能再使用它了。我只是期望在iterator 上执行toStream 时会执行一种逻辑,例如在不触及迭代器本身的情况下将流“链接”到迭代器。好的..
【问题讨论】: