From 4a2e3d417557e38ccb09404e93701454f6e11e37 Mon Sep 17 00:00:00 2001 From: Aleksandar Pokopec Date: Tue, 16 Mar 2010 14:23:13 +0000 Subject: Fixes infinite streams in #3091. No review. --- src/library/scala/collection/immutable/Stream.scala | 10 ++++++++++ test/files/pos/scan.scala | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 6c55131175..33e5b70f90 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -119,6 +119,16 @@ self => else new Stream.Cons(head, (tail ++ that).asInstanceOf[Stream[A]])).asInstanceOf[That] } + /** + * Create a new stream which contains all intermediate results of applying the operator + * to subsequent elements left to right. + * @note This works because the target type of the Builder That is a Stream. + */ + override final def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[Stream[A], B, That]): That = { + (if (this.isEmpty) Stream(z) + else new Stream.Cons(z, tail.scanLeft(op(z, head))(op).asInstanceOf[Stream[B]])).asInstanceOf[That] + } + /** Create a new stream which contains all elements of this stream * followed by all elements of Iterator `that' */ diff --git a/test/files/pos/scan.scala b/test/files/pos/scan.scala index a88abe63d2..47e0a7d976 100644 --- a/test/files/pos/scan.scala +++ b/test/files/pos/scan.scala @@ -13,6 +13,11 @@ object Test { val emp = List[Int]() assert(emp.scanLeft(0)(_ + _) == List(0)) assert(emp.scanRight(0)(_ + _) == List(0)) + + val stream = Stream(1, 2, 3, 4, 5) + assert(stream.scanLeft(0)(_ + _) == Stream(0, 1, 3, 6, 10, 15)) + + assert(Stream.from(1).scanLeft(0)(_ + _).take(5) == Stream(0, 1, 3, 6, 10)) } } \ No newline at end of file -- cgit v1.2.3