diff options
-rw-r--r-- | src/library/scala/collection/immutable/Stream.scala | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 50b086a881..26fa86861c 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -324,6 +324,22 @@ self => else tail.foldLeft(op(z, head))(op) } + /** Stream specialization of reduceLeft which allows GC to collect + * along the way. + */ + override final def reduceLeft[B >: A](f: (B, A) => B): B = { + if (this.isEmpty) throw new UnsupportedOperationException("empty.reduceLeft") + else { + var reducedRes: B = this.head + var left = this.tail + while (!left.isEmpty) { + reducedRes = f(reducedRes, left.head) + left = left.tail + } + reducedRes + } + } + /** Returns all the elements of this stream that satisfy the * predicate <code>p</code>. The order of the elements is preserved. * |