diff options
author | Paul Phillips <paulp@improving.org> | 2010-09-15 23:12:32 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-09-15 23:12:32 +0000 |
commit | 8562015759b9f99547a78886e28da79e588eaf18 (patch) | |
tree | 5cfa349c03dac668e40bcf56e3f075d4d170727a /src | |
parent | e595d0a130df434ee006911476f087277158308e (diff) | |
download | scala-8562015759b9f99547a78886e28da79e588eaf18.tar.gz scala-8562015759b9f99547a78886e28da79e588eaf18.tar.bz2 scala-8562015759b9f99547a78886e28da79e588eaf18.zip |
Stream gets a specialized, extra-lazy Iterator ...
Stream gets a specialized, extra-lazy Iterator so it can iterate as
lazily as it traverses. Patch submitted by "Eastsun": probably not
his real name, but that's what we call him. Closes #3273, review by
prokopec.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/immutable/Stream.scala | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index cf9a48983a..5339b29e7c 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -245,6 +245,30 @@ self => new StreamWithFilter(x => p(x) && q(x)) } + /** See #3273 and test case run/bug3273 for motivation. */ + final class StreamIterator extends Iterator[A] { + // A call-by-need cell. + class LazyCell(st: => Stream[A]) { + lazy val v = st + } + + private var these = new LazyCell(self) + def hasNext: Boolean = these.v.nonEmpty + def next: A = + if (isEmpty) Iterator.empty.next + else { + val cur = these.v + val result = cur.head + these = new LazyCell(cur.tail) + result + } + override def toStream = these.v + override def toList = toStream.toList + } + + /** A lazier Iterator than LinearSeqLike's. */ + override def iterator: Iterator[A] = new StreamIterator + /** Apply the given function <code>f</code> to each element of this linear sequence * (while respecting the order of the elements). * |