From f3d87c08f6bcdb864e6990194668ad6dc16826a9 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 1 Jun 2010 17:44:33 +0000 Subject: Make Iterator.toStream be properly lazy. --- src/library/scala/collection/Iterator.scala | 3 +++ src/library/scala/collection/TraversableLike.scala | 6 ++++++ src/library/scala/collection/TraversableOnce.scala | 14 +++++++------- 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') 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. -- cgit v1.2.3