summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-08-26 04:53:25 +0000
committerPaul Phillips <paulp@improving.org>2009-08-26 04:53:25 +0000
commit48cc8408cfd0b57f46746162b8d4c184f7a3ed5c (patch)
tree92c5be40f62cfd5387c50b7917cbf000103ddf96
parent7224d1c26def0519cab9c31c77f02f0ab5734838 (diff)
downloadscala-48cc8408cfd0b57f46746162b8d4c184f7a3ed5c.tar.gz
scala-48cc8408cfd0b57f46746162b8d4c184f7a3ed5c.tar.bz2
scala-48cc8408cfd0b57f46746162b8d4c184f7a3ed5c.zip
Tail recursive foldLeft for Stream.
-rw-r--r--src/library/scala/collection/immutable/Stream.scala11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala
index 2841beea14..e6f3d3c47c 100644
--- a/src/library/scala/collection/immutable/Stream.scala
+++ b/src/library/scala/collection/immutable/Stream.scala
@@ -13,6 +13,7 @@ package scala.collection.immutable
import scala.collection.mutable.ListBuffer
import scala.collection.generic._
+import scala.annotation.tailrec
/**
* <p>The class <code>Stream</code> implements lazy lists where elements
@@ -180,6 +181,7 @@ self =>
* for allowing the GC to collect the underlying stream as elements are
* consumed.
*/
+ @tailrec
override final def foreach[B](f: A => B) {
if (!this.isEmpty) {
f(head)
@@ -187,6 +189,15 @@ self =>
}
}
+ /** Stream specialization of foldLeft which allows GC to collect
+ * along the way.
+ */
+ @tailrec
+ override final def foldLeft[B](z: B)(op: (B, A) => B): B = {
+ if (this.isEmpty) z
+ else tail.foldLeft(op(z, head))(op)
+ }
+
/** Returns all the elements of this stream that satisfy the
* predicate <code>p</code>. The order of the elements is preserved.
*