Fixes infinite streams in #3091. No review.

git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@21188 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
prokopec 2010-03-16 14:23:13 +00:00
parent 0f1a5978db
commit f36d3eea61
2 changed files with 15 additions and 0 deletions

View File

@ -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'
*/

View File

@ -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))
}
}