diff options
author | Josh Suereth <joshua.suereth@gmail.com> | 2012-06-18 11:43:18 -0400 |
---|---|---|
committer | Josh Suereth <joshua.suereth@gmail.com> | 2012-06-18 11:43:56 -0400 |
commit | f559505c1f254c98ae34bb4cdf1e2b624d25a84c (patch) | |
tree | dc1297564648f326a4361e44fdff9fdc440671b0 /src/library/scala/collection/Iterator.scala | |
parent | 89f83568709c01348d34262fa9e408a252c6300e (diff) | |
download | scala-f559505c1f254c98ae34bb4cdf1e2b624d25a84c.tar.gz scala-f559505c1f254c98ae34bb4cdf1e2b624d25a84c.tar.bz2 scala-f559505c1f254c98ae34bb4cdf1e2b624d25a84c.zip |
Adding copyInto and toVector methods to collections.
* Added generic copyInto method for collections. For any collection with a CanBuildFrom, can convert a generic collection into it using the builder.
* Added specifici toVector method for collections. This is more efficient than copyInto if the collection is a Vector.
Diffstat (limited to 'src/library/scala/collection/Iterator.scala')
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index b2bbc8d888..62449c7712 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -11,6 +11,8 @@ package scala.collection import mutable.ArrayBuffer import annotation.migration import immutable.Stream +import scala.collection.generic.CanBuildFrom +import scala.annotation.unchecked.{ uncheckedVariance => uV } /** The `Iterator` object provides various functions for creating specialized iterators. * @@ -1139,6 +1141,14 @@ trait Iterator[+A] extends TraversableOnce[A] { if (self.hasNext) Stream.cons(self.next, self.toStream) else Stream.empty[A] + def toVector: Vector[A] = copyInto[Vector] + def copyInto[Col[_]](implicit cbf: CanBuildFrom[Nothing, A, Col[A @uV]]): Col[A @uV] = { + val b = cbf() + while(hasNext) b += next + b.result + } + + /** Converts this iterator to a string. * * @return `"empty iterator"` or `"non-empty iterator"`, depending on |