summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-06-01 17:44:33 +0000
committerPaul Phillips <paulp@improving.org>2010-06-01 17:44:33 +0000
commitf3d87c08f6bcdb864e6990194668ad6dc16826a9 (patch)
treee2ce1bae13f4e9a62b267884dcb7824329b46388 /src
parent8acca208ae541e44f306a50c210e2626f571332b (diff)
downloadscala-f3d87c08f6bcdb864e6990194668ad6dc16826a9.tar.gz
scala-f3d87c08f6bcdb864e6990194668ad6dc16826a9.tar.bz2
scala-f3d87c08f6bcdb864e6990194668ad6dc16826a9.zip
Make Iterator.toStream be properly lazy.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/collection/Iterator.scala3
-rw-r--r--src/library/scala/collection/TraversableLike.scala6
-rw-r--r--src/library/scala/collection/TraversableOnce.scala14
3 files changed, 16 insertions, 7 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index daa6a59f3b..b8dd03110d 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -1007,6 +1007,9 @@ trait Iterator[+A] extends TraversableOnce[A] {
def toTraversable: Traversable[A] = toStream
def toIterator: Iterator[A] = self
+ def toStream: Stream[A] =
+ if (self.hasNext) Stream.cons(self.next, self.toStream)
+ else Stream.empty[A]
/** Converts this iterator to a string.
* @return `"empty iterator"` or `"non-empty iterator"`, depending on whether or not the iterator is empty.
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala
index 50259a1ee8..88a6379da2 100644
--- a/src/library/scala/collection/TraversableLike.scala
+++ b/src/library/scala/collection/TraversableLike.scala
@@ -696,6 +696,12 @@ trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with Traversable
def toTraversable: Traversable[A] = thisCollection
def toIterator: Iterator[A] = toIterable.iterator
+ /** Converts this $coll to a stream.
+ * $willNotTerminateInf
+ * @return a stream containing all elements of this $coll.
+ */
+ def toStream: Stream[A] = toIterable.toStream
+
/** Converts this $coll to a string.
* @return a string representation of this collection. By default this
* string consists of the `stringPrefix` of this $coll,
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala
index 3984264168..1797ff39db 100644
--- a/src/library/scala/collection/TraversableOnce.scala
+++ b/src/library/scala/collection/TraversableOnce.scala
@@ -66,6 +66,12 @@ trait TraversableOnce[+A] {
*/
def toTraversable: Traversable[A]
+ /** Converts this $coll to a stream.
+ * $willNotTerminateInf
+ * @return a stream containing all elements of this $coll.
+ */
+ def toStream: Stream[A]
+
/** Presently these are abstract because the Traversable versions use
* breakable/break, and I wasn't sure enough of how that's supposed to
* function to consolidate them with the Iterator versions.
@@ -383,7 +389,7 @@ trait TraversableOnce[+A] {
copyToArray(result, 0)
result
}
- else toStream.toArray
+ else toBuffer.toArray
}
/** Converts this $coll to a list.
@@ -416,12 +422,6 @@ trait TraversableOnce[+A] {
*/
def toBuffer[B >: A]: mutable.Buffer[B] = new ArrayBuffer[B] ++= self
- /** Converts this $coll to a stream.
- * $willNotTerminateInf
- * @return a stream containing all elements of this $coll.
- */
- def toStream: Stream[A] = toList.toStream
-
/** Converts this $coll to a set.
* $willNotTerminateInf
* @return a set containing all elements of this $coll.