在 scala 2.7.5 中对迭代器进行分区
看起来 scala 2.7.5 中的 Iterator
上没有 partition
方法(2.8 中有)。我希望在不失去迭代器懒惰的情况下进行分区,因此以下不是选项:
itr.toList.partition( someTest(_) )
任何人都可以推荐一种方法吗在不实现我自己的 partition
方法的情况下执行此操作?例如,是否有某种方法可以将 Iterator
转换为延迟评估的 Stream
?
It looks as though there is no partition
method on an Iterator
in scala 2.7.5 (there is in 2.8). I'd like to have a partition without losing the laziness of the Iterator
, so the following is not an option:
itr.toList.partition( someTest(_) )
Can anyone recommend a way of doing this without implementing my own partition
method? For example, is there some way of converting an Iterator
into a lazily-evaluated Stream
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过
Stream.fromIterator
方法?它生成一个包含给定迭代器的元素的流:)。一个例子是:
希望它有帮助(这就是你想要的)。
编辑:只是一个例子来表明这是懒惰的(并且是记忆的 - 就像所有
Stream
一样)。注意:遗漏了一些输出,因为它非常冗长。
——弗拉维·西普西甘
Have you tried the
Stream.fromIterator
method? It produces a stream containing the elements of the given iterator :).An example would be:
Hope it helps (and it is the thing you had in mind).
EDIT: Just an example to show that this is lazy (and memoised - as all
Stream
s are).NB: left some output out as it was quite verbose.
-- Flaviu Cipcigan