diff options
-rw-r--r-- | src/library/scala/collection/immutable/Stream.scala | 24 | ||||
-rw-r--r-- | test/files/run/bug3273.scala | 10 |
2 files changed, 34 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). * diff --git a/test/files/run/bug3273.scala b/test/files/run/bug3273.scala new file mode 100644 index 0000000000..379a8a29c1 --- /dev/null +++ b/test/files/run/bug3273.scala @@ -0,0 +1,10 @@ +object Test { + val num1: Stream[Int] = 1 #:: num1.map(_ + 1) + val num2: Stream[Int] = 1 #:: num2.iterator.map(_ + 1).toStream + + def main(args: Array[String]): Unit = { + val x1 = (num1 take 10).toList + val x2 = (num2 take 10).toList + assert(x1 == x2) + } +} |