开发者

Is there a data structure like stream, but weak?

Weak as in weak references. Basically, I need a sequence of numbers where some of th开发者_开发技巧em can be unallocated when they aren't needed anymore.


scalaz.EphemeralStream is what you want.


Views provide you with a lazy collection, where each value is computed as it is needed.


One thing you could do is create an Iterable instead of a Stream. Your Iterable needs to provide an iterator method, which returns an iterator with hasNext and next methods.

When you loop over the Iterable, hasNext and next will be called to generate the elements as they are needed, but they are not stored anywhere (like a Stream does).

Simple example:

class Numbers extends Iterable[Int] {
  def iterator = new Iterator[Int] {
    private var num = -1

    def hasNext = num < 99
    def next = { num += 1; num }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜