diff options
author | Philipp Haller <hallerp@gmail.com> | 2011-03-08 15:31:46 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2011-03-08 15:31:46 +0000 |
commit | fff27219456d145cdb67502dd591dee32b01ce89 (patch) | |
tree | f4fe43725a6403ac0d0a748a07b2a353ce062596 /src/library/scala/collection/immutable/Stream.scala | |
parent | 6dfc61ab724ca949c3d48384fc51b960a3662158 (diff) | |
download | scala-fff27219456d145cdb67502dd591dee32b01ce89.tar.gz scala-fff27219456d145cdb67502dd591dee32b01ce89.tar.bz2 scala-fff27219456d145cdb67502dd591dee32b01ce89.zip |
Closes #2239.
Diffstat (limited to 'src/library/scala/collection/immutable/Stream.scala')
-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. * |