summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/immutable/Stream.scala
diff options
context:
space:
mode:
authorIulian Dragos <jaguarul@gmail.com>2009-08-25 10:00:50 +0000
committerIulian Dragos <jaguarul@gmail.com>2009-08-25 10:00:50 +0000
commitd7f5a8824a94d01cb76f459454932d71b9e6f5cf (patch)
tree61c68099229a29a73d7f81d91b5d1f3516afce87 /src/library/scala/collection/immutable/Stream.scala
parenta75d39a04db312f500de055a847a082380fee287 (diff)
downloadscala-d7f5a8824a94d01cb76f459454932d71b9e6f5cf.tar.gz
scala-d7f5a8824a94d01cb76f459454932d71b9e6f5cf.tar.bz2
scala-d7f5a8824a94d01cb76f459454932d71b9e6f5cf.zip
Added 'foreach' method in Stream that is tail-c...
Added 'foreach' method in Stream that is tail-call optimized, and which does not hold references to stream elements (fix for #692).
Diffstat (limited to 'src/library/scala/collection/immutable/Stream.scala')
-rw-r--r--src/library/scala/collection/immutable/Stream.scala16
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 badeb31bbc..2841beea14 100644
--- a/src/library/scala/collection/immutable/Stream.scala
+++ b/src/library/scala/collection/immutable/Stream.scala
@@ -171,6 +171,22 @@ self =>
else new Stream.Cons(rest.head, rest.tail filter p)
}
+ /** Apply the given function <code>f</code> to each element of this linear sequence
+ * (while respecting the order of the elements).
+ *
+ * @param f the treatment to apply to each element.
+ * @note Overridden here as final to trigger tail-call optimization, which replaces
+ * 'this' with 'tail' at each iteration. This is absolutely necessary
+ * for allowing the GC to collect the underlying stream as elements are
+ * consumed.
+ */
+ override final def foreach[B](f: A => B) {
+ if (!this.isEmpty) {
+ f(head)
+ tail.foreach(f)
+ }
+ }
+
/** Returns all the elements of this stream that satisfy the
* predicate <code>p</code>. The order of the elements is preserved.
*