summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/io/Directory.scala1
-rw-r--r--src/compiler/scala/tools/nsc/io/File.scala1
-rw-r--r--src/compiler/scala/tools/nsc/io/ZipArchive.scala1
-rw-r--r--src/compiler/scala/tools/nsc/matching/Matrix.scala2
-rw-r--r--src/library/scala/collection/Iterator.scala333
-rw-r--r--src/library/scala/collection/MapLike.scala14
-rw-r--r--src/library/scala/collection/Traversable.scala3
-rw-r--r--src/library/scala/collection/TraversableLike.scala479
-rw-r--r--src/library/scala/collection/TraversableOnce.scala519
-rw-r--r--src/library/scala/collection/TraversableProxyLike.scala3
-rw-r--r--src/library/scala/collection/TraversableViewLike.scala6
-rw-r--r--src/library/scala/collection/generic/Addable.scala9
-rw-r--r--src/library/scala/collection/generic/Growable.scala13
-rw-r--r--src/library/scala/collection/generic/Shrinkable.scala9
-rw-r--r--src/library/scala/collection/generic/Subtractable.scala11
-rw-r--r--src/library/scala/collection/immutable/List.scala5
-rw-r--r--src/library/scala/collection/immutable/MapLike.scala12
-rw-r--r--src/library/scala/collection/immutable/SortedMap.scala12
-rw-r--r--src/library/scala/collection/immutable/Stack.scala14
-rw-r--r--src/library/scala/collection/immutable/Stream.scala10
-rw-r--r--src/library/scala/collection/interfaces/MapMethods.scala3
-rw-r--r--src/library/scala/collection/interfaces/SetMethods.scala6
-rw-r--r--src/library/scala/collection/interfaces/TraversableMethods.scala3
-rw-r--r--src/library/scala/collection/interfaces/TraversableOnceMethods.scala69
-rw-r--r--src/library/scala/collection/mutable/ArrayBuffer.scala6
-rw-r--r--src/library/scala/collection/mutable/ArrayBuilder.scala20
-rw-r--r--src/library/scala/collection/mutable/ArrayStack.scala10
-rw-r--r--src/library/scala/collection/mutable/BufferLike.scala59
-rw-r--r--src/library/scala/collection/mutable/BufferProxy.scala11
-rw-r--r--src/library/scala/collection/mutable/Builder.scala3
-rw-r--r--src/library/scala/collection/mutable/LazyBuilder.scala5
-rw-r--r--src/library/scala/collection/mutable/ListBuffer.scala2
-rw-r--r--src/library/scala/collection/mutable/MapLike.scala30
-rw-r--r--src/library/scala/collection/mutable/PriorityQueue.scala10
-rw-r--r--src/library/scala/collection/mutable/PriorityQueueProxy.scala12
-rw-r--r--src/library/scala/collection/mutable/QueueProxy.scala13
-rw-r--r--src/library/scala/collection/mutable/SetLike.scala26
-rw-r--r--src/library/scala/collection/mutable/Stack.scala17
-rw-r--r--src/library/scala/collection/mutable/StackProxy.scala25
-rw-r--r--src/library/scala/collection/mutable/SynchronizedBuffer.scala24
-rw-r--r--src/library/scala/collection/mutable/SynchronizedPriorityQueue.scala14
-rw-r--r--src/library/scala/collection/mutable/SynchronizedQueue.scala10
-rw-r--r--src/library/scala/collection/mutable/SynchronizedSet.scala16
-rw-r--r--src/library/scala/collection/mutable/SynchronizedStack.scala10
-rw-r--r--src/library/scala/package.scala2
-rw-r--r--src/library/scala/util/Random.scala14
-rw-r--r--test/files/lib/scalacheck-1.7-SNAPSHOT.jar.desired.sha12
-rw-r--r--test/files/pos/spec-List.scala12
48 files changed, 735 insertions, 1156 deletions
diff --git a/src/compiler/scala/tools/nsc/io/Directory.scala b/src/compiler/scala/tools/nsc/io/Directory.scala
index 2c7f5e1059..5355fcf681 100644
--- a/src/compiler/scala/tools/nsc/io/Directory.scala
+++ b/src/compiler/scala/tools/nsc/io/Directory.scala
@@ -10,7 +10,6 @@ package scala.tools.nsc
package io
import java.io.{ File => JFile }
-import collection.Traversable
object Directory {
import scala.util.Properties.{ tmpDir, userHome, userDir }
diff --git a/src/compiler/scala/tools/nsc/io/File.scala b/src/compiler/scala/tools/nsc/io/File.scala
index 67d39fd742..7f2c736408 100644
--- a/src/compiler/scala/tools/nsc/io/File.scala
+++ b/src/compiler/scala/tools/nsc/io/File.scala
@@ -15,7 +15,6 @@ import java.io.{
FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
BufferedInputStream, BufferedOutputStream, IOException, PrintStream, File => JFile }
import java.nio.channels.{ Channel, FileChannel }
-import collection.Traversable
import scala.io.Codec
object File
diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
index 9d062a45f1..f4af0f5a8f 100644
--- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala
+++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala
@@ -14,7 +14,6 @@ import java.io.{ File => JFile, IOException, InputStream, BufferedInputStream, B
import java.util.zip.{ ZipEntry, ZipFile, ZipInputStream }
import PartialFunction._
-import scala.collection.Traversable
import scala.collection.mutable.{ Map, HashMap }
import scala.collection.JavaConversions.asIterator
diff --git a/src/compiler/scala/tools/nsc/matching/Matrix.scala b/src/compiler/scala/tools/nsc/matching/Matrix.scala
index 88507efa2f..de3204318f 100644
--- a/src/compiler/scala/tools/nsc/matching/Matrix.scala
+++ b/src/compiler/scala/tools/nsc/matching/Matrix.scala
@@ -49,7 +49,7 @@ trait Matrix extends MatrixAdditions {
The is the real work-horse of the algorithm. There is some column whose top-most pattern is a
constructor. (Forsimplicity, itisdepicted above asthe left-most column, but anycolumn will do.)
The goal is to build a test state with the variablevand some outgoing arcs (one for each construc-
- tor and possibly a default arc). Foreach constructorcin the selected column, its arc is defined as
+ tor and possibly a default arc). Foreach constructor in the selected column, its arc is defined as
follows:
Let {i1,...,ij} be the rows-indices of the patterns in the column that match c. Since the pat-
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index 8510592ceb..a8e99836dc 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -145,6 +145,27 @@ object Iterator {
def next = elem
}
+ /** With the advent of TraversableOnce, it can be useful to have a builder
+ * for Iterators so they can be treated uniformly along with the collections.
+ * See scala.util.Random.shuffle for an example.
+ */
+ class IteratorCanBuildFrom[A] extends generic.CanBuildFrom[Iterator[A], A, Iterator[A]] {
+ def newIterator = new ArrayBuffer[A] mapResult (_.iterator)
+
+ /** Creates a new builder on request of a collection.
+ * @param from the collection requesting the builder to be created.
+ * @return the result of invoking the `genericBuilder` method on `from`.
+ */
+ def apply(from: Iterator[A]) = newIterator
+
+ /** Creates a new builder from scratch
+ * @return the result of invoking the `newBuilder` method of this factory.
+ */
+ def apply() = newIterator
+ }
+
+ implicit def iteratorCanBuildFrom[T]: IteratorCanBuildFrom[T] = new IteratorCanBuildFrom[T]
+
/** A wrapper class for the `flatten` method that is added to
* class `Iterator` with implicit conversion
* @see iteratorIteratorWrapper.
@@ -265,7 +286,8 @@ import Iterator.empty
* @define mayNotTerminateInf
* Note: may not terminate for infinite iterators.
*/
-trait Iterator[+A] { self =>
+trait Iterator[+A] extends TraversableOnce[A] {
+ self =>
/** Tests whether this iterator can provide another element.
* @return `true` if a subsequent call to `next` will yield an element,
@@ -284,6 +306,17 @@ trait Iterator[+A] { self =>
*/
def isEmpty: Boolean = !hasNext
+ /** Tests whether this Iterator can be repeatedly traversed.
+ * @return `false`
+ */
+ def isTraversableAgain = false
+
+ /** Tests whether this Iterator has a known size.
+ *
+ * @return `true` for empty Iterators, `false` otherwise.
+ */
+ def hasDefiniteSize = isEmpty
+
/** Selects first ''n'' values of this iterator.
* @param n the number of values to take
* @return an iterator producing only of the first `n` values of this iterator, or else the
@@ -697,131 +730,6 @@ trait Iterator[+A] { self =>
if (found) i else -1
}
- /** Applies a binary operator to a start value and all values produced by this iterator, going left to right.
- * $willNotTerminateInf
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going left to right with the start value `z` on the left:
- * {{{
- * op(...op(z, x,,1,,), x,,2,,, ..., x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- */
- def foldLeft[B](z: B)(op: (B, A) => B): B = {
- var acc = z
- while (hasNext) acc = op(acc, next())
- acc
- }
-
- /** Applies a binary operator to all values produced by this iterator and a start value, going right to left.
- * $willNotTerminateInf
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going right to left with the start value `z` on the right:
- * {{{
- * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- */
- def foldRight[B](z: B)(op: (A, B) => B): B =
- if (hasNext) op(next(), foldRight(z)(op)) else z
-
- /** Applies a binary operator to a start value and all values produced by this iterator, going left to right.
- *
- * Note: `/:` is alternate syntax for `foldLeft`; `z /: it` is the same as `it foldLeft z`.
- * $willNotTerminateInf
- *
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going left to right with the start value `z` on the left:
- * {{{
- * op(...op(z, x,,1,,), x,,2,,, ..., x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- */
- def /:[B](z: B)(op: (B, A) => B): B = foldLeft(z)(op)
-
- /** Applies a binary operator to all values produced by this iterator and a start value, going right to left.
- * Note: `:\` is alternate syntax for `foldRight`; `it :\ z` is the same as `it foldRight z`.
- * $willNotTerminateInf
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going right to left with the start value `z` on the right:
- * {{{
- * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- */
- def :\[B](z: B)(op: (A, B) => B): B = foldRight(z)(op)
-
- /** Applies a binary operator to all values produced by this iterator, going left to right.
- * $willNotTerminateInf
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going left to right:
- * {{{
- * op(...(op(x,,1,,, x,,2,,), ... ) , x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- * @throws `UnsupportedOperationException` if this iterator is empty.
- */
- def reduceLeft[B >: A](op: (B, A) => B): B = {
- if (hasNext) foldLeft[B](next())(op)
- else throw new UnsupportedOperationException("empty.reduceLeft")
- }
-
- /** Applies a binary operator to all values produced by this iterator, going right to left.
- * $willNotTerminateInf
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive values produced by this iterator
- * going right to left:
- * {{{
- * op(x,,1,,, op(x,,2,,, ..., op(x,,n-1,,, x,,n,,)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the values produced by this iterator.
- * @throws `UnsupportedOperationException` if this iterator is empty.
- */
- def reduceRight[B >: A](op: (A, B) => B): B = {
- if (hasNext) foldRight[B](next())(op)
- else throw new UnsupportedOperationException("empty.reduceRight")
- }
-
- /** Optionally applies a binary operator to all values produced by this iterator, going left to right.
- * $willNotTerminateInf
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return an option value containing the result of `reduceLeft(op)` is this iterator is nonempty,
- * `None` otherwise.
- */
- def reduceLeftOption[B >: A](op: (B, A) => B): Option[B] = {
- if (!hasNext) None else Some(reduceLeft(op))
- }
-
- /** Optionally applies a binary operator to all values produced by this iterator, going right to left.
- * $willNotTerminateInf
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return an option value containing the result of `reduceRight(op)` is this iterator is nonempty,
- * `None` otherwise.
- */
- def reduceRightOption[B >: A](op: (A, B) => B): Option[B] = {
- if (!hasNext) None else Some(reduceRight(op))
- }
-
/** Creates a buffered iterator from this iterator.
* @see BufferedIterator
* @return a buffered iterator producing the same values as this iterator.
@@ -996,13 +904,7 @@ trait Iterator[+A] { self =>
*
* Note: The iterator is at its end after this method returns.
*/
- def length: Int = {
- var i = 0
- while (hasNext) {
- next(); i += 1
- }
- i
- }
+ def length: Int = this.size
/** Creates two new iterators that both iterate over the same elements
* as this iterator (in the same order). The duplicate iterators are
@@ -1083,99 +985,6 @@ trait Iterator[+A] { self =>
}
}
- /** Copies values produced by this iterator to an array.
- * Fills the given array `xs` with values produced by this iterator, after skipping `start` values.
- * Copying will stop once either the end of the current iterator is reached,
- * or the end of the array is reached.
- *
- * $willNotTerminateInf
- *
- * @param xs the array to fill.
- * @param start the starting index.
- * @tparam B the type of the elements of the array.
- *
- * @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
- */
- def copyToArray[B >: A](xs: Array[B], start: Int): Unit =
- copyToArray(xs, start, xs.length - start)
-
- /** Copies values produced by this iterator to an array.
- * Fills the given array `xs` with values produced by this iterator.
- * Copying will stop once either the end of the current iterator is reached,
- * or the end of the array is reached.
- *
- * $willNotTerminateInf
- *
- * @param xs the array to fill.
- * @tparam B the type of the elements of the array.
- *
- * @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
- */
- def copyToArray[B >: A](xs: Array[B]): Unit = copyToArray(xs, 0, xs.length)
-
- /** Copies all values produced by this iterator to a buffer.
- * $willNotTerminateInf
- * @param dest The buffer to which elements are copied
- */
- def copyToBuffer[B >: A](dest: Buffer[B]) {
- while (hasNext) dest += next()
- }
-
- /** Traverses this iterator and returns all produced values in a list.
- * $willNotTerminateInf
- *
- * @return a list which contains all values produced by this iterator.
- */
- def toList: List[A] = {
- val res = new ListBuffer[A]
- while (hasNext) res += next
- res.toList
- }
-
- /** Traverses this iterator and returns all produced values in a set.
- * $willNotTerminateInf
- *
- * @return a set which contains all values produced by this iterator.
- */
- def toSet[B >: A]: immutable.Set[B] = {
- val res = new ListBuffer[B]
- while (hasNext) res += next
- res.toSet
- }
-
- /** Lazily wraps a Stream around this iterator so its values are memoized.
- *
- * @return a Stream which can repeatedly produce all the values
- * produced by this iterator.
- */
- def toStream: Stream[A] =
- if (hasNext) Stream.cons(next, toStream) else Stream.empty
-
- /** Traverses this iterator and returns all produced values in a sequence.
- * $willNotTerminateInf
- *
- * @return a list which contains all values produced by this iterator.
- */
- def toSeq: Seq[A] = {
- val buffer = new ArrayBuffer[A]
- this copyToBuffer buffer
- buffer
- }
-
- /** Traverses this iterator and returns all produced values in a map.
- * $willNotTerminateInf
- * @see TraversableLike.toMap
- *
- * @return a map containing all elements of this iterator.
- */
- def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
- val b = immutable.Map.newBuilder[T, U]
- while (hasNext)
- b += next
-
- b.result
- }
-
/** Tests if another iterator produces the same valeus as this one.
* $willNotTerminateInf
* @param that the other iterator
@@ -1189,76 +998,8 @@ trait Iterator[+A] { self =>
!hasNext && !that.hasNext
}
- /** Displays all values produced by this iterator in a string using start, end, and separator strings.
- *
- * @param start the starting string.
- * @param sep the separator string.
- * @param end the ending string.
- * @return a string representation of this iterator. The resulting string
- * begins with the string `start` and ends with the string
- * `end`. Inside, the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator are separated by the string `sep`.
- */
- def mkString(start: String, sep: String, end: String): String = {
- val buf = new StringBuilder
- addString(buf, start, sep, end).toString
- }
-
- /** Displays all values produced by this iterator in a string using a separator string.
- *
- * @param sep the separator string.
- * @return a string representation of this iterator. In the resulting string
- * the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator are separated by the string `sep`.
- */
- def mkString(sep: String): String = mkString("", sep, "")
-
- /** Displays all values produced by this iterator in a string.
- * @return a string representation of this iterator. In the resulting string
- * the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator follow each other without any separator string.
- */
- def mkString: String = mkString("")
-
- /** Appends all values produced by this iterator to a string builder using start, end, and separator strings.
- * The written text begins with the string `start` and ends with the string
- * `end`. Inside, the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator are separated by the string `sep`.
- *
- * @param b the string builder to which elements are appended.
- * @param start the starting string.
- * @param sep the separator string.
- * @param end the ending string.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(buf: StringBuilder, start: String, sep: String, end: String): StringBuilder = {
- buf.append(start)
- val elems = this
- if (elems.hasNext) buf.append(elems.next)
- while (elems.hasNext) {
- buf.append(sep); buf.append(elems.next)
- }
- buf.append(end)
- }
-
- /** Appends all values produced by this iterator to a string builder using a separator string.
- * The written text consists of the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator, separated by the string `sep`.
- *
- * @param b the string builder to which elements are appended.
- * @param sep the separator string.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(buf: StringBuilder, sep: String): StringBuilder = addString(buf, "", sep, "")
-
- /** Appends all values produced by this iterator to a string builder.
- * The written text consists of the string representations (w.r.t. the method `toString`)
- * of all values produced by this iterator without any separator string.
- *
- * @param b the string builder to which elements are appended.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(buf: StringBuilder): StringBuilder = addString(buf, "", "", "")
+ def toTraversable: Traversable[A] = toStream
+ def toIterator: Iterator[A] = self
/** 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/MapLike.scala b/src/library/scala/collection/MapLike.scala
index 88c2b69a10..f43f7a2ca5 100644
--- a/src/library/scala/collection/MapLike.scala
+++ b/src/library/scala/collection/MapLike.scala
@@ -286,18 +286,8 @@ self =>
* @return a new map with the given bindings added to this map
* @usecase def + (kvs: Traversable[(A, B)]): Map[A, B]
*/
- def ++[B1 >: B](kvs: Traversable[(A, B1)]): Map[A, B1] =
- ((repr: Map[A, B1]) /: kvs) (_ + _)
-
- /** Adds all key/value pairs produced by an iterator to this map, returning a new map.
- *
- * @param iter the iterator producing key/value pairs
- * @tparam B1 the type of the added values
- * @return a new map with the given bindings added to this map
- * @usecase def + (iter: Iterator[(A, B)]): Map[A, B]
- */
- def ++[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1] =
- ((repr: Map[A, B1]) /: iter) (_ + _)
+ def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1] =
+ ((repr: Map[A, B1]) /: xs) (_ + _)
/** Returns a new map with all key/value pairs for which the predicate
* <code>p</code> returns <code>true</code>.
diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala
index c3d7fa8bc7..1cecec5227 100644
--- a/src/library/scala/collection/Traversable.scala
+++ b/src/library/scala/collection/Traversable.scala
@@ -29,8 +29,7 @@ trait Traversable[+A] extends TraversableLike[A, Traversable[A]]
override def isEmpty: Boolean
override def size: Int
override def hasDefiniteSize
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Traversable[A], B, That]): That
- override def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[Traversable[A], B, That]): That
+ override def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[Traversable[A], B, That]): That
override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Traversable[A], B, That]): That
override def flatMap[B, That](f: A => Traversable[B])(implicit bf: CanBuildFrom[Traversable[A], B, That]): That
override def filter(p: A => Boolean): Traversable[A]
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala
index b5b3e1c05e..7008d3b5fd 100644
--- a/src/library/scala/collection/TraversableLike.scala
+++ b/src/library/scala/collection/TraversableLike.scala
@@ -88,8 +88,8 @@ import immutable.{List, Stream, Nil, ::}
*
* Note: will not terminate for infinite-sized collections.
*/
-trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] {
-self =>
+trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] with TraversableOnce[A] {
+ self =>
import Traversable.breaks._
@@ -148,23 +148,10 @@ self =>
result
}
- /** Tests whether the $coll is not empty.
- *
- * @return `true` if the $coll contains at least one element, `false` otherwise.
- */
- def nonEmpty: Boolean = !isEmpty
-
- /** The size of this $coll.
- *
- * $willNotTerminateInf
- *
- * @return the number of elements in this $coll.
+ /** Tests whether this $coll can be repeatedly traversed.
+ * @return `true`
*/
- def size: Int = {
- var result = 0
- for (x <- this) result += 1
- result
- }
+ final def isTraversableAgain = true
/** Tests whether this $coll is known to have a finite size.
* All strict collections are known to have finite size. For a non-strict collection
@@ -186,36 +173,15 @@ self =>
* @return a new collection of type `That` which contains all elements of this $coll
* followed by all elements of `that`.
*
- * @usecase def ++(that: Traversable[A]): $Coll[A]
- *
- * @return a new $coll which contains all elements of this $coll
- * followed by all elements of `that`.
- */
- def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
- val b = bf(repr)
- b ++= thisCollection
- b ++= that
- b.result
- }
-
- /** Concatenates this $coll with the elements of an iterator.
- *
- * @param that the iterator to append.
- * @tparam B the element type of the returned collection.
- * @tparam That $thatinfo
- * @param bf $bfinfo
- * @return a new collection of type `That` which contains all elements of this $coll
- * followed by all elements of `that`.
- *
- * @usecase def ++(that: Iterator[A]): $Coll[A]
+ * @usecase def ++(that: TraversableOnce[A]): $Coll[A]
*
* @return a new $coll which contains all elements of this $coll
* followed by all elements of `that`.
*/
- def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
+ def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
val b = bf(repr)
b ++= thisCollection
- b ++= that
+ b ++= xs
b.result
}
@@ -409,21 +375,6 @@ self =>
result
}
- /** Counts the number of elements in the $coll which satisfy a predicate.
- *
- * @param p the predicate used to test elements.
- * @return the number of elements satisfying the predicate `p`.
- *
- *
- */
- def count(p: A => Boolean): Int = {
- var cnt = 0
- for (x <- this) {
- if (p(x)) cnt += 1
- }
- cnt
- }
-
/** Finds the first element of the $coll satisfying a predicate, if any.
*
* $mayNotTerminateInf
@@ -464,156 +415,6 @@ self =>
}
*/
- /** Applies a binary operator to a start value and all elements of this $coll, going left to right.
- *
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going left to right with the start value `z` on the left:
- * {{{
- * op(...op(z, x,,1,,), x,,2,,, ..., x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- */
- def foldLeft[B](z: B)(op: (B, A) => B): B = {
- var result = z
- for (x <- this)
- result = op(result, x)
- result
- }
-
- /** Applies a binary operator to a start value and all elements of this $coll, going left to right.
- *
- * Note: `/:` is alternate syntax for `foldLeft`; `z /: xs` is the same as `xs foldLeft z`.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going left to right with the start value `z` on the left:
- * {{{
- * op(...op(op(z, x,,1,,), x,,2,,), ..., x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- */
- def /: [B](z: B)(op: (B, A) => B): B = foldLeft(z)(op)
-
- /** Applies a binary operator to all elements of this $coll and a start value, going right to left.
- *
- * $willNotTerminateInf
- * $orderDependentFold
- * @param z the start value.
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going right to left with the start value `z` on the right:
- * {{{
- * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- */
- def foldRight[B](z: B)(op: (A, B) => B): B = reversed.foldLeft(z)((x, y) => op(y, x))
-
- private def reversed: List[A] = {
- var elems: List[A] = Nil
- for (x <- this) elems = x :: elems
- elems
- }
-
- /** Applies a binary operator to all elements of this $coll and a start value, going right to left.
- *
- * Note: `:\` is alternate syntax for `foldRight`; `xs :\ z` is the same as `xs foldRight z`.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param z the start value
- * @param op the binary operator
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going right to left with the start value `z` on the right:
- * {{{
- * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- */
- def :\ [B](z: B)(op: (A, B) => B): B = foldRight(z)(op)
-
- /** Applies a binary operator to all elements of this $coll, going left to right.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going left to right:
- * {{{
- * op(...(op(x,,1,,, x,,2,,), ... ) , x,,n,,)
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- * @throws `UnsupportedOperationException` if this $coll is empty.
- */
- def reduceLeft[B >: A](op: (B, A) => B): B = {
- if (isEmpty) throw new UnsupportedOperationException("empty.reduceLeft")
- var result: B = head
- var first = true
- for (x <- this)
- if (first) first = false
- else result = op(result, x)
- result
- }
-
- /** Optionally applies a binary operator to all elements of this $coll, going left to right.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return an option value containing the result of `reduceLeft(op)` is this $coll is nonempty,
- * `None` otherwise.
- */
- def reduceLeftOption[B >: A](op: (B, A) => B): Option[B] = {
- if (isEmpty) None else Some(reduceLeft(op))
- }
-
- /** Applies a binary operator to all elements of this $coll, going right to left.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return the result of inserting `op` between consecutive elements of this $coll$,
- * going right to left:
- * {{{
- * op(x,,1,,, op(x,,2,,, ..., op(x,,n-1,,, x,,n,,)...))
- * }}}
- * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
- * @throws `UnsupportedOperationException` if this $coll is empty.
- */
- def reduceRight[B >: A](op: (A, B) => B): B = {
- if (isEmpty) throw new UnsupportedOperationException("empty.reduceRight")
- var elems: List[A] = Nil
- for (x <- this) elems = x :: elems
- elems.reduceLeft[B]((x, y) => op(y, x))
- }
-
- /** Optionally applies a binary operator to all elements of this $coll, going right to left.
- * $willNotTerminateInf
- * $orderDependentFold
- *
- * @param op the binary operator.
- * @tparam B the result type of the binary operator.
- * @return an option value containing the result of `reduceRight(op)` is this $coll is nonempty,
- * `None` otherwise.
- */
- def reduceRightOption[B >: A](op: (A, B) => B): Option[B] =
- if (isEmpty) None else Some(reduceRight(op))
-
/**
* Produces a collection containing cummulative results of applying the operator going left to right.
* $willNotTerminateInf
@@ -654,81 +455,6 @@ self =>
b.result
}
- /** Sums up the elements of this collection.
- *
- * @param num an implicit parameter defining a set of numeric operations
- * which includes the `+` operator to be used in forming the sum.
- * @tparam B the result type of the `+` operator.
- * @return the sum of all elements of this $coll with respect to the `+` operator in `num`.
- *
- * @usecase def sum: Int
- *
- * @return the sum of all elements in this $coll of numbers of type `Int`.
- * Instead of `Int`, any other type `T` with an implicit `Numeric[T]` implementation
- * can be used as element type of the $coll and as result type of `sum`.
- * Examples of such types are: `Long`, `Float`, `Double`, `BigInt`.
- *
- */
- def sum[B >: A](implicit num: Numeric[B]): B = {
- var acc = num.zero
- for (x <- self) acc = num.plus(acc, x)
- acc
- }
-
- /** Multiplies up the elements of this collection.
- *
- * @param num an implicit parameter defining a set of numeric operations
- * which includes the `*` operator to be used in forming the product.
- * @tparam B the result type of the `*` operator.
- * @return the product of all elements of this $coll with respect to the `*` operator in `num`.
- *
- * @usecase def product: Int
- *
- * @return the product of all elements in this $coll of numbers of type `Int`.
- * Instead of `Int`, any other type `T` with an implicit `Numeric[T]` implementation
- * can be used as element type of the $coll and as result type of `product`.
- * Examples of such types are: `Long`, `Float`, `Double`, `BigInt`.
- */
- def product[B >: A](implicit num: Numeric[B]): B = {
- var acc = num.one
- for (x <- self) acc = num.times(acc, x)
- acc
- }
-
- /** Finds the smallest element.
- *
- * @param cmp An ordering to be used for comparing elements.
- * @tparam B The type over which the ordering is defined.
- * @return the smallest element of this $coll with respect to the ordering `cmp`.
- *
- * @usecase def min: A
- * @return the smallest element of this $coll
- */
- def min[B >: A](implicit cmp: Ordering[B]): A = {
- if (isEmpty) throw new UnsupportedOperationException("empty.min")
- var acc = self.head
- for (x <- self)
- if (cmp.lt(x, acc)) acc = x
- acc
- }
-
- /** Finds the largest element.
- *
- * @param cmp An ordering to be used for comparing elements.
- * @tparam B The type over which the ordering is defined.
- * @return the largest element of this $coll with respect to the ordering `cmp`.
- *
- * @usecase def max: A
- * @return the largest element of this $coll.
- */
- def max[B >: A](implicit cmp: Ordering[B]): A = {
- if (isEmpty) throw new UnsupportedOperationException("empty.max")
- var acc = self.head
- for (x <- self)
- if (cmp.gt(x, acc)) acc = x
- acc
- }
-
/** Selects the first element of this $coll.
* $orderDependent
* @return the first element of this $coll.
@@ -931,14 +657,6 @@ self =>
(l.result, r.result)
}
- /** Copies all elements of this $coll to a buffer.
- * $willNotTerminateInf
- * @param dest The buffer to which elements are copied.
- */
- def copyToBuffer[B >: A](dest: Buffer[B]) {
- for (x <- this) dest += x
- }
-
/** Copies elements of this $coll to an array.
* Fills the given array `xs` with at most `len` elements of
* this $coll, starting at position `start`.
@@ -967,185 +685,8 @@ self =>
}
}
- /** Copies elements of this $coll to an array.
- * Fills the given array `xs` with all elements of
- * this $coll, starting at position `start`.
- * Copying will stop once either the end of the current $coll is reached,
- * or the end of the array is reached.
- *
- * $willNotTerminateInf
- *
- * @param xs the array to fill.
- * @param start the starting index.
- * @tparam B the type of the elements of the array.
- *
- * @usecase def copyToArray(xs: Array[A], start: Int): Unit
- */
- def copyToArray[B >: A](xs: Array[B], start: Int) {
- copyToArray(xs, start, xs.length - start)
- }
-
- /** Copies elements of this $coll to an array.
- * Fills the given array `xs` with all elements of
- * this $coll, starting at position `0`.
- * Copying will stop once either the end of the current $coll is reached,
- * or the end of the array is reached.
- *
- * $willNotTerminateInf
- *
- * @param xs the array to fill.
- * @tparam B the type of the elements of the array.
- *
- * @usecase def copyToArray(xs: Array[A], start: Int): Unit
- */
- def copyToArray[B >: A](xs: Array[B]) {
- copyToArray(xs, 0)
- }
-
- /** Converts this $coll to an array.
- * $willNotTerminateInf
- *
- * @tparam B the type of the elements of the array. A `ClassManifest` for this type must
- * be available.
- * @return an array containing all elements of this $coll.
- *
- * @usecase def toArray: Array[A]
- * @return an array containing all elements of this $coll.
- * A `ClassManifest` must be available for the element type of this $coll.
- */
- def toArray[B >: A : ClassManifest]: Array[B] = {
- val result = new Array[B](size)
- copyToArray(result, 0)
- result
- }
-
- /** Converts this $coll to a list.
- * $willNotTerminateInf
- * @return a list containing all elements of this $coll.
- */
- def toList: List[A] = (new ListBuffer[A] ++= thisCollection).toList
-
- /** Converts this $coll to an iterable collection.
- * $willNotTerminateInf
- * @return an `Iterable` containing all elements of this $coll.
- */
- def toIterable: Iterable[A] = toStream
-
- /** Converts this $coll to a sequence.
- * $willNotTerminateInf
- * @return a sequence containing all elements of this $coll.
- */
- def toSeq: Seq[A] = toList
-
- /** Converts this $coll to an indexed sequence.
- * $willNotTerminateInf
- * @return an indexed sequence containing all elements of this $coll.
- */
- def toIndexedSeq[B >: A]: mutable.IndexedSeq[B] = (new ArrayBuffer[B] ++= thisCollection)
-
- /** 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.
- */
- def toSet[B >: A]: immutable.Set[B] = immutable.Set() ++ thisCollection
-
- /** Converts this $coll to a map. This method is unavailable unless
- * the elements are members of Tuple2, each ((K, V)) becoming a key-value
- * pair in the map. Duplicate keys will be overwritten by later keys:
- * if this is an unordered collection, which key is in the resulting map
- * is undefined.
- * $willNotTerminateInf
- * @return a map containing all elements of this $coll.
- */
- def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
- val b = immutable.Map.newBuilder[T, U]
- for (x <- this)
- b += x
-
- b.result
- }
-
- /** Displays all elements of this $coll in a string using start, end, and separator strings.
- *
- * @param start the starting string.
- * @param sep the separator string.
- * @param end the ending string.
- * @return a string representation of this $coll. The resulting string
- * begins with the string `start` and ends with the string
- * `end`. Inside, the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll are separated by the string `sep`.
- *
- * @example `List(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"`
- */
- def mkString(start: String, sep: String, end: String): String =
- addString(new StringBuilder(), start, sep, end).toString
-
- /** Displays all elements of this $coll in a string using a separator string.
- *
- * @param sep the separator string.
- * @return a string representation of this $coll. In the resulting string
- * the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll are separated by the string `sep`.
- *
- * @example `List(1, 2, 3).mkString("|") = "1|2|3"`
- */
- def mkString(sep: String): String =
- addString(new StringBuilder(), sep).toString
-
- /** Displays all elements of this $coll in a string.
- * @return a string representation of this $coll. In the resulting string
- * the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll follow each other without any separator string.
- */
- def mkString: String =
- addString(new StringBuilder()).toString
-
- /** Appends all elements of this $coll to a string builder using start, end, and separator strings.
- * The written text begins with the string `start` and ends with the string
- * `end`. Inside, the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll are separated by the string `sep`.
- *
- * @param b the string builder to which elements are appended.
- * @param start the starting string.
- * @param sep the separator string.
- * @param end the ending string.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder = {
- b append start
- var first = true
- for (x <- this) {
- if (first) first = false
- else b append sep
- b append x
- }
- b append end
- }
-
- /** Appends all elements of this $coll to a string builder using a separator string.
- * The written text consists of the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll, separated by the string `sep`.
- *
- * @param b the string builder to which elements are appended.
- * @param sep the separator string.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(b: StringBuilder, sep: String): StringBuilder = addString(b, "", sep, "")
-
- /** Appends all elements of this $coll to a string builder.
- * The written text consists of the string representations (w.r.t. the method `toString`)
- * of all elements of this $coll without any separator string.
- *
- * @param b the string builder to which elements are appended.
- * @return the string builder `b` to which elements were appended.
- */
- def addString(b: StringBuilder): StringBuilder = addString(b, "")
+ def toTraversable: Traversable[A] = thisCollection
+ def toIterator: Iterator[A] = toIterable.iterator
/** Converts this $coll to a string
* @return a string representation of this collection. By default this
diff --git a/src/library/scala/collection/TraversableOnce.scala b/src/library/scala/collection/TraversableOnce.scala
new file mode 100644
index 0000000000..2224f534ad
--- /dev/null
+++ b/src/library/scala/collection/TraversableOnce.scala
@@ -0,0 +1,519 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.collection
+
+import mutable.{ Buffer, ListBuffer, ArrayBuffer }
+
+/** A template trait for collections which can be traversed one
+ * or more times.
+ * $traversableonceinfo
+ *
+ * @tparam A the element type of the collection
+ *
+ * @define traversableonceinfo
+ * This trait is composed of those methods which can be implemented
+ * solely in terms of foreach and which do not need access to a Builder.
+ * It represents the implementations common to Iterators and
+ * Traversables, such as folds, conversions, and other operations which
+ * traverse some or all of the elements and return a derived value.
+ *
+ * @author Martin Odersky
+ * @author Paul Phillips
+ * @version 2.8
+ * @since 2.8
+ *
+ * @define coll traversable or iterator
+ * @define orderDependentFold
+ *
+ * Note: might return different results for different runs, unless the underlying collection type is ordered.
+ * or the operator is associative and commutative.
+ * @define willNotTerminateInf
+ *
+ * Note: will not terminate for infinite-sized collections.
+ */
+trait TraversableOnce[+A] {
+ self =>
+
+ /** Self-documenting abstract methods. */
+ def foreach[U](f: A => U): Unit
+ def isEmpty: Boolean
+ def hasDefiniteSize: Boolean
+
+ /** Tests whether this $coll can be repeatedly traversed. Always
+ * true for Traversables and false for Iterators unless overridden.
+ *
+ * @return `true` if it is repeatedly traversable, `false` otherwise.
+ */
+ def isTraversableAgain: Boolean
+
+ /** Returns an Iterator over the elements in this $coll. Will return
+ * the same Iterator if this instance is already an Iterator.
+ * $willNotTerminateInf
+ * @return an Iterator containing all elements of this $coll.
+ */
+ def toIterator: Iterator[A]
+
+ /** Converts this $coll to an unspecified Traversable. Will return
+ * the same collection if this instance is already Traversable.
+ * $willNotTerminateInf
+ * @return a Traversable containing all elements of this $coll.
+ */
+ def toTraversable: Traversable[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.
+ */
+ def forall(p: A => Boolean): Boolean
+ def exists(p: A => Boolean): Boolean
+ def find(p: A => Boolean): Option[A]
+ def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit
+ // def mapFind[B](f: A => Option[B]): Option[B]
+
+ // for internal use
+ protected[this] def reversed = {
+ var elems: List[A] = Nil
+ self foreach (elems ::= _)
+ elems
+ }
+
+ /** The size of this $coll.
+ *
+ * $willNotTerminateInf
+ *
+ * @return the number of elements in this $coll.
+ */
+ def size: Int = {
+ var result = 0
+ for (x <- self) result += 1
+ result
+ }
+
+ /** Tests whether the $coll is not empty.
+ *
+ * @return `true` if the $coll contains at least one element, `false` otherwise.
+ */
+ def nonEmpty: Boolean = !isEmpty
+
+ /** Counts the number of elements in the $coll which satisfy a predicate.
+ *
+ * @param p the predicate used to test elements.
+ * @return the number of elements satisfying the predicate `p`.
+ */
+ def count(p: A => Boolean): Int = {
+ var cnt = 0
+ for (x <- this)
+ if (p(x)) cnt += 1
+
+ cnt
+ }
+
+ /** Applies a binary operator to a start value and all elements of this $coll, going left to right.
+ *
+ * Note: `/:` is alternate syntax for `foldLeft`; `z /: xs` is the same as `xs foldLeft z`.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param z the start value.
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going left to right with the start value `z` on the left:
+ * {{{
+ * op(...op(op(z, x,,1,,), x,,2,,), ..., x,,n,,)
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ */
+ def /:[B](z: B)(op: (B, A) => B): B = foldLeft(z)(op)
+
+ /** Applies a binary operator to all elements of this $coll and a start value, going right to left.
+ *
+ * Note: `:\` is alternate syntax for `foldRight`; `xs :\ z` is the same as `xs foldRight z`.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param z the start value
+ * @param op the binary operator
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going right to left with the start value `z` on the right:
+ * {{{
+ * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ */
+ def :\[B](z: B)(op: (A, B) => B): B = foldRight(z)(op)
+
+ /** Applies a binary operator to a start value and all elements of this $coll, going left to right.
+ *
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param z the start value.
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going left to right with the start value `z` on the left:
+ * {{{
+ * op(...op(z, x,,1,,), x,,2,,, ..., x,,n,,)
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ */
+ def foldLeft[B](z: B)(op: (B, A) => B): B = {
+ var result = z
+ this foreach (x => result = op(result, x))
+ result
+ }
+
+ /** Applies a binary operator to all elements of this $coll and a start value, going right to left.
+ *
+ * $willNotTerminateInf
+ * $orderDependentFold
+ * @param z the start value.
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going right to left with the start value `z` on the right:
+ * {{{
+ * op(x,,1,,, op(x,,2,,, ... op(x,,n,,, z)...))
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ */
+ def foldRight[B](z: B)(op: (A, B) => B): B =
+ reversed.foldLeft(z)((x, y) => op(y, x))
+
+ /** Applies a binary operator to all elements of this $coll, going left to right.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going left to right:
+ * {{{
+ * op(...(op(x,,1,,, x,,2,,), ... ) , x,,n,,)
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ * @throws `UnsupportedOperationException` if this $coll is empty.
+ */
+ def reduceLeft[B >: A](op: (B, A) => B): B = {
+ if (isEmpty)
+ throw new UnsupportedOperationException("empty.reduceLeft")
+
+ var first = true
+ var acc: B = 0.asInstanceOf[B]
+
+ for (x <- self) {
+ if (first) {
+ acc = x
+ first = false
+ }
+ else acc = op(acc, x)
+ }
+ acc
+ }
+
+ /** Applies a binary operator to all elements of this $coll, going right to left.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return the result of inserting `op` between consecutive elements of this $coll$,
+ * going right to left:
+ * {{{
+ * op(x,,1,,, op(x,,2,,, ..., op(x,,n-1,,, x,,n,,)...))
+ * }}}
+ * where `x,,1,,, ..., x,,n,,` are the elements of this $coll.
+ * @throws `UnsupportedOperationException` if this $coll is empty.
+ */
+ def reduceRight[B >: A](op: (A, B) => B): B = {
+ if (isEmpty)
+ throw new UnsupportedOperationException("empty.reduceRight")
+
+ reversed.reduceLeft[B]((x, y) => op(y, x))
+ }
+
+ /** Optionally applies a binary operator to all elements of this $coll, going left to right.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return an option value containing the result of `reduceLeft(op)` is this $coll is nonempty,
+ * `None` otherwise.
+ */
+ def reduceLeftOption[B >: A](op: (B, A) => B): Option[B] =
+ if (isEmpty) None else Some(reduceLeft(op))
+
+ /** Optionally applies a binary operator to all elements of this $coll, going right to left.
+ * $willNotTerminateInf
+ * $orderDependentFold
+ *
+ * @param op the binary operator.
+ * @tparam B the result type of the binary operator.
+ * @return an option value containing the result of `reduceRight(op)` is this $coll is nonempty,
+ * `None` otherwise.
+ */
+ def reduceRightOption[B >: A](op: (A, B) => B): Option[B] =
+ if (isEmpty) None else Some(reduceRight(op))
+
+ /** Sums up the elements of this collection.
+ *
+ * @param num an implicit parameter defining a set of numeric operations
+ * which includes the `+` operator to be used in forming the sum.
+ * @tparam B the result type of the `+` operator.
+ * @return the sum of all elements of this $coll with respect to the `+` operator in `num`.
+ *
+ * @usecase def sum: Int
+ *
+ * @return the sum of all elements in this $coll of numbers of type `Int`.
+ * Instead of `Int`, any other type `T` with an implicit `Numeric[T]` implementation
+ * can be used as element type of the $coll and as result type of `sum`.
+ * Examples of such types are: `Long`, `Float`, `Double`, `BigInt`.
+ *
+ */
+ def sum[B >: A](implicit num: Numeric[B]): B = foldLeft(num.zero)(num.plus)
+
+ /** Multiplies up the elements of this collection.
+ *
+ * @param num an implicit parameter defining a set of numeric operations
+ * which includes the `*` operator to be used in forming the product.
+ * @tparam B the result type of the `*` operator.
+ * @return the product of all elements of this $coll with respect to the `*` operator in `num`.
+ *
+ * @usecase def product: Int
+ *
+ * @return the product of all elements in this $coll of numbers of type `Int`.
+ * Instead of `Int`, any other type `T` with an implicit `Numeric[T]` implementation
+ * can be used as element type of the $coll and as result type of `product`.
+ * Examples of such types are: `Long`, `Float`, `Double`, `BigInt`.
+ */
+ def product[B >: A](implicit num: Numeric[B]): B = foldLeft(num.one)(num.times)
+
+ /** Finds the smallest element.
+ *
+ * @param cmp An ordering to be used for comparing elements.
+ * @tparam B The type over which the ordering is defined.
+ * @return the smallest element of this $coll with respect to the ordering `cmp`.
+ *
+ * @usecase def min: A
+ * @return the smallest element of this $coll
+ */
+ def min[B >: A](implicit cmp: Ordering[B]): A = {
+ if (isEmpty)
+ throw new UnsupportedOperationException("empty.min")
+
+ reduceLeft((x, y) => if (cmp.lteq(x, y)) x else y)
+ }
+
+ /** Finds the largest element.
+ *
+ * @param cmp An ordering to be used for comparing elements.
+ * @tparam B The type over which the ordering is defined.
+ * @return the largest element of this $coll with respect to the ordering `cmp`.
+ *
+ * @usecase def max: A
+ * @return the largest element of this $coll.
+ */
+ def max[B >: A](implicit cmp: Ordering[B]): A = {
+ if (isEmpty)
+ throw new UnsupportedOperationException("empty.max")
+
+ reduceLeft((x, y) => if (cmp.gteq(x, y)) x else y)
+ }
+
+ /** Copies all elements of this $coll to a buffer.
+ * $willNotTerminateInf
+ * @param dest The buffer to which elements are copied.
+ */
+ def copyToBuffer[B >: A](dest: Buffer[B]): Unit = dest ++= self
+
+ /** Copies values produced by this iterator to an array.
+ * Fills the given array `xs` with values produced by this iterator, after skipping `start` values.
+ * Copying will stop once either the end of the current iterator is reached,
+ * or the end of the array is reached.
+ *
+ * $willNotTerminateInf
+ *
+ * @param xs the array to fill.
+ * @param start the starting index.
+ * @tparam B the type of the elements of the array.
+ *
+ * @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
+ */
+ def copyToArray[B >: A](xs: Array[B], start: Int): Unit =
+ copyToArray(xs, start, xs.length - start)
+
+ /** Copies values produced by this iterator to an array.
+ * Fills the given array `xs` with values produced by this iterator.
+ * Copying will stop once either the end of the current iterator is reached,
+ * or the end of the array is reached.
+ *
+ * $willNotTerminateInf
+ *
+ * @param xs the array to fill.
+ * @tparam B the type of the elements of the array.
+ *
+ * @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
+ */
+ def copyToArray[B >: A](xs: Array[B]): Unit =
+ copyToArray(xs, 0, xs.length)
+
+ /** Converts this $coll to an array.
+ * $willNotTerminateInf
+ *
+ * @tparam B the type of the elements of the array. A `ClassManifest` for this type must
+ * be available.
+ * @return an array containing all elements of this $coll.
+ *
+ * @usecase def toArray: Array[A]
+ * @return an array containing all elements of this $coll.
+ * A `ClassManifest` must be available for the element type of this $coll.
+ */
+ def toArray[B >: A : ClassManifest]: Array[B] = {
+ val result = new Array[B](size)
+ copyToArray(result, 0)
+ result
+ }
+
+ /** Converts this $coll to a list.
+ * $willNotTerminateInf
+ * @return a list containing all elements of this $coll.
+ */
+ def toList: List[A] = new ListBuffer[A] ++= self toList
+
+ /** Converts this $coll to an iterable collection.
+ * $willNotTerminateInf
+ * @return an `Iterable` containing all elements of this $coll.
+ */
+ def toIterable: Iterable[A] = toStream
+
+ /** Converts this $coll to a sequence.
+ * $willNotTerminateInf
+ * @return a sequence containing all elements of this $coll.
+ */
+ def toSeq: Seq[A] = toList
+
+ /** Converts this $coll to an indexed sequence.
+ * $willNotTerminateInf
+ * @return an indexed sequence containing all elements of this $coll.
+ */
+ def toIndexedSeq[B >: A]: mutable.IndexedSeq[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.
+ */
+ def toSet[B >: A]: immutable.Set[B] = immutable.Set() ++ self
+
+ /** Converts this $coll to a map. This method is unavailable unless
+ * the elements are members of Tuple2, each ((K, V)) becoming a key-value
+ * pair in the map. Duplicate keys will be overwritten by later keys:
+ * if this is an unordered collection, which key is in the resulting map
+ * is undefined.
+ * $willNotTerminateInf
+ * @return a map containing all elements of this $coll.
+ */
+ def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
+ val b = immutable.Map.newBuilder[T, U]
+ for (x <- self)
+ b += x
+
+ b.result
+ }
+
+ /** Displays all elements of this $coll in a string using start, end, and separator strings.
+ *
+ * @param start the starting string.
+ * @param sep the separator string.
+ * @param end the ending string.
+ * @return a string representation of this $coll. The resulting string
+ * begins with the string `start` and ends with the string
+ * `end`. Inside, the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll are separated by the string `sep`.
+ *
+ * @example `List(1, 2, 3).mkString("(", "; ", ")") = "(1; 2; 3)"`
+ */
+ def mkString(start: String, sep: String, end: String): String =
+ addString(new StringBuilder(), start, sep, end).toString
+
+ /** Displays all elements of this $coll in a string using a separator string.
+ *
+ * @param sep the separator string.
+ * @return a string representation of this $coll. In the resulting string
+ * the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll are separated by the string `sep`.
+ *
+ * @example `List(1, 2, 3).mkString("|") = "1|2|3"`
+ */
+ def mkString(sep: String): String = mkString("", sep, "")
+
+ /** Displays all elements of this $coll in a string.
+ * @return a string representation of this $coll. In the resulting string
+ * the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll follow each other without any separator string.
+ */
+ def mkString: String = mkString("")
+
+ /** Appends all elements of this $coll to a string builder using start, end, and separator strings.
+ * The written text begins with the string `start` and ends with the string
+ * `end`. Inside, the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll are separated by the string `sep`.
+ *
+ * @param b the string builder to which elements are appended.
+ * @param start the starting string.
+ * @param sep the separator string.
+ * @param end the ending string.
+ * @return the string builder `b` to which elements were appended.
+ */
+ def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder = {
+ var first = true
+
+ b append start
+ for (x <- self) {
+ if (first) {
+ b append x
+ first = false
+ }
+ else {
+ b append sep
+ b append x
+ }
+ }
+ b append end
+
+ b
+ }
+
+ /** Appends all elements of this $coll to a string builder using a separator string.
+ * The written text consists of the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll, separated by the string `sep`.
+ *
+ * @param b the string builder to which elements are appended.
+ * @param sep the separator string.
+ * @return the string builder `b` to which elements were appended.
+ */
+ def addString(b: StringBuilder, sep: String): StringBuilder = addString(b, "", sep, "")
+
+ /** Appends all elements of this $coll to a string builder.
+ * The written text consists of the string representations (w.r.t. the method `toString`)
+ * of all elements of this $coll without any separator string.
+ *
+ * @param b the string builder to which elements are appended.
+ * @return the string builder `b` to which elements were appended.
+ */
+ def addString(b: StringBuilder): StringBuilder = addString(b, "")
+}
diff --git a/src/library/scala/collection/TraversableProxyLike.scala b/src/library/scala/collection/TraversableProxyLike.scala
index e435393b38..fb8da98a6b 100644
--- a/src/library/scala/collection/TraversableProxyLike.scala
+++ b/src/library/scala/collection/TraversableProxyLike.scala
@@ -32,8 +32,7 @@ trait TraversableProxyLike[+A, +Repr <: TraversableLike[A, Repr] with Traversabl
override def nonEmpty: Boolean = self.nonEmpty
override def size: Int = self.size
override def hasDefiniteSize = self.hasDefiniteSize
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = self.++(that)(bf)
- override def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = self.++(that)(bf)
+ override def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = self.++(xs)(bf)
override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = self.map(f)(bf)
override def flatMap[B, That](f: A => Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = self.flatMap(f)(bf)
override def filter(p: A => Boolean): Repr = self.filter(p)
diff --git a/src/library/scala/collection/TraversableViewLike.scala b/src/library/scala/collection/TraversableViewLike.scala
index 233125c0f3..6bf32dd71f 100644
--- a/src/library/scala/collection/TraversableViewLike.scala
+++ b/src/library/scala/collection/TraversableViewLike.scala
@@ -157,14 +157,12 @@ self =>
protected def newDroppedWhile(p: A => Boolean): Transformed[A] = new DroppedWhile { val pred = p }
protected def newTakenWhile(p: A => Boolean): Transformed[A] = new TakenWhile { val pred = p }
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[This, B, That]): That = {
- newAppended(that).asInstanceOf[That]
+ override def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[This, B, That]): That = {
+ newAppended(xs.toTraversable).asInstanceOf[That]
// was: if (bf.isInstanceOf[ByPassCanBuildFrom]) newAppended(that).asInstanceOf[That]
// else super.++[B, That](that)(bf)
}
- override def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[This, B, That]): That = ++[B, That](that.toStream)
-
override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[This, B, That]): That = {
newMapped(f).asInstanceOf[That]
// val b = bf(repr)
diff --git a/src/library/scala/collection/generic/Addable.scala b/src/library/scala/collection/generic/Addable.scala
index 5cbc7951cf..ecbd8301b6 100644
--- a/src/library/scala/collection/generic/Addable.scala
+++ b/src/library/scala/collection/generic/Addable.scala
@@ -52,12 +52,5 @@ trait Addable[A, +Repr <: Addable[A, Repr]] { self =>
* @param elems the collection containing the added elements.
* @return a new $coll with the given elements added.
*/
- def ++ (elems: Traversable[A]): Repr = (repr /: elems) (_ + _)
-
- /** Creates a new $coll by adding all elements produced by an iterator to this $coll.
- *
- * @param iter the iterator producing the added elements.
- * @return a new $coll with the given elements added.
- */
- def ++ (iter: Iterator[A]): Repr = (repr /: iter) (_ + _)
+ def ++ (xs: TraversableOnce[A]): Repr = (repr /: xs) (_ + _)
}
diff --git a/src/library/scala/collection/generic/Growable.scala b/src/library/scala/collection/generic/Growable.scala
index 93fe1fcb59..80f933a901 100644
--- a/src/library/scala/collection/generic/Growable.scala
+++ b/src/library/scala/collection/generic/Growable.scala
@@ -41,19 +41,12 @@ trait Growable[-A] {
*/
def +=(elem1: A, elem2: A, elems: A*): this.type = this += elem1 += elem2 ++= elems
- /** ${Add}s all elements produced by an iterator to this $coll.
+ /** ${Add}s all elements produced by a TraversableOnce to this $coll.
*
- * @param iter the iterator producing the elements to $add.
+ * @param iter the TraversableOnce producing the elements to $add.
* @return the $coll itself.
*/
- def ++=(iter: Iterator[A]): this.type = { iter foreach += ; this }
-
- /** ${Add}s all elements contained in a traversable collection to this $coll.
- *
- * @param elems the collection containing the elements to $add.
- * @return the $coll itself.
- */
- def ++=(elems: Traversable[A]): this.type = { elems foreach +=; this }
+ def ++=(xs: TraversableOnce[A]): this.type = { xs foreach += ; this }
/** Clears the $coll's contents. After this operation, the
* $coll is empty.
diff --git a/src/library/scala/collection/generic/Shrinkable.scala b/src/library/scala/collection/generic/Shrinkable.scala
index fafeb51141..cf970e1232 100644
--- a/src/library/scala/collection/generic/Shrinkable.scala
+++ b/src/library/scala/collection/generic/Shrinkable.scala
@@ -47,14 +47,7 @@ trait Shrinkable[-A] {
* @param iter the iterator producing the elements to remove.
* @return the $coll itself
*/
- def --=(iter: Iterator[A]): this.type = { iter foreach -=; this }
-
- /** Removes all elements contained in a traversable collection from this $coll.
- *
- * @param iter the collection containing the elements to remove.
- * @return the $coll itself
- */
- def --=(iter: Traversable[A]): this.type = { iter foreach -=; this }
+ def --=(xs: TraversableOnce[A]): this.type = { xs foreach -= ; this }
}
diff --git a/src/library/scala/collection/generic/Subtractable.scala b/src/library/scala/collection/generic/Subtractable.scala
index 8ded9c22d1..b2051d2773 100644
--- a/src/library/scala/collection/generic/Subtractable.scala
+++ b/src/library/scala/collection/generic/Subtractable.scala
@@ -56,14 +56,5 @@ trait Subtractable[A, +Repr <: Subtractable[A, Repr]] { self =>
* @return a new $coll that contains all elements of the current $coll
* except one less occurrence of each of the elements of `elems`.
*/
- def --(elems: Traversable[A]): Repr = (repr /: elems) (_ - _)
-
- /** Creates a new $coll from this $coll by removing all elements produced
- * by an iterator.
- *
- * @param iter the iterator producing the removed elements.
- * @return a new $coll that contains all elements of the current $coll
- * except one less occurrence of each of the elements produced by `iter`.
- */
- def --(iter: Iterator[A]): Repr = (repr /: iter) (_ - _)
+ def --(xs: TraversableOnce[A]): Repr = (repr /: xs) (_ - _)
}
diff --git a/src/library/scala/collection/immutable/List.scala b/src/library/scala/collection/immutable/List.scala
index e3e8c8963e..3ccf6ee1ce 100644
--- a/src/library/scala/collection/immutable/List.scala
+++ b/src/library/scala/collection/immutable/List.scala
@@ -135,15 +135,12 @@ sealed abstract class List[+A] extends LinearSeq[A]
// Overridden methods from IterableLike and SeqLike or overloaded variants of such methods
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[List[A], B, That]): That = {
+ override def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[List[A], B, That]): That = {
val b = bf(this)
if (b.isInstanceOf[ListBuffer[_]]) (this ::: that.toList).asInstanceOf[That]
else super.++(that)
}
- override def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[List[A], B, That]): That =
- this ++ that.toList
-
override def +:[B >: A, That](elem: B)(implicit bf: CanBuildFrom[List[A], B, That]): That = bf match {
case _: List.GenericCanBuildFrom[_] => (elem :: this).asInstanceOf[That]
case _ => super.+:(elem)(bf)
diff --git a/src/library/scala/collection/immutable/MapLike.scala b/src/library/scala/collection/immutable/MapLike.scala
index 0a3a8a3645..662321bb0c 100644
--- a/src/library/scala/collection/immutable/MapLike.scala
+++ b/src/library/scala/collection/immutable/MapLike.scala
@@ -75,16 +75,8 @@ trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]]
*
* @param elems the traversable object.
*/
- override def ++[B1 >: B](elems: Traversable[(A, B1)]): immutable.Map[A, B1] =
- ((repr: immutable.Map[A, B1]) /: elems) (_ + _)
-
- /** Adds a number of elements provided by an iterator
- * and returns a new collection with the added elements.
- *
- * @param iter the iterator
- */
- override def ++[B1 >: B] (iter: Iterator[(A, B1)]): immutable.Map[A, B1] =
- ((repr: immutable.Map[A, B1]) /: iter) (_ + _)
+ override def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): immutable.Map[A, B1] =
+ ((repr: immutable.Map[A, B1]) /: xs) (_ + _)
/** Filters this map by retaining only keys satisfying a predicate.
* @param p the predicate used to test keys
diff --git a/src/library/scala/collection/immutable/SortedMap.scala b/src/library/scala/collection/immutable/SortedMap.scala
index baf7f5451e..919b529a49 100644
--- a/src/library/scala/collection/immutable/SortedMap.scala
+++ b/src/library/scala/collection/immutable/SortedMap.scala
@@ -58,16 +58,8 @@ trait SortedMap[A, +B] extends Map[A, B]
*
* @param elems the traversable object.
*/
- override def ++[B1 >: B](elems: scala.collection.Traversable[(A, B1)]): SortedMap[A, B1] =
- ((repr: SortedMap[A, B1]) /: elems) (_ + _)
-
- /** Adds a number of elements provided by an iterator
- * and returns a new collection with the added elements.
- *
- * @param iter the iterator
- */
- override def ++[B1 >: B] (iter: Iterator[(A, B1)]): SortedMap[A, B1] =
- ((repr: SortedMap[A, B1]) /: iter) (_ + _)
+ override def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): SortedMap[A, B1] =
+ ((repr: SortedMap[A, B1]) /: xs) (_ + _)
}
/**
diff --git a/src/library/scala/collection/immutable/Stack.scala b/src/library/scala/collection/immutable/Stack.scala
index a5d7e9515a..afbf5d720e 100644
--- a/src/library/scala/collection/immutable/Stack.scala
+++ b/src/library/scala/collection/immutable/Stack.scala
@@ -74,18 +74,8 @@ class Stack[+A] protected (protected val elems: List[A]) extends LinearSeq[A]
* @param elems the iterator object.
* @return the stack with the new elements on top.
*/
- def pushAll[B >: A](elems: Iterator[B]): Stack[B] =
- ((this: Stack[B]) /: elems)(_ push _)
-
- /** Push all elements provided by the given traversable object onto
- * the stack. The last element returned by the iterable object
- * will be on top of the new stack.
- *
- * @param elems the iterable object.
- * @return the stack with the new elements on top.
- */
- def pushAll[B >: A](elems: scala.collection.Traversable[B]): Stack[B] =
- ((this: Stack[B]) /: elems)(_ push _)
+ def pushAll[B >: A](xs: TraversableOnce[B]): Stack[B] =
+ ((this: Stack[B]) /: xs.toIterator)(_ push _)
/** Returns the top element of the stack. An error is signaled if
* there is no element on the stack.
diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala
index 33e5b70f90..39e6363e30 100644
--- a/src/library/scala/collection/immutable/Stream.scala
+++ b/src/library/scala/collection/immutable/Stream.scala
@@ -113,7 +113,7 @@ self =>
* then StreamBuilder will be chosen for the implicit.
* we recognize that fact and optimize to get more laziness.
*/
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Stream[A], B, That]): That = {
+ override def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Stream[A], B, That]): That = {
// we assume there is no other builder factory on streams and therefore know that That = Stream[A]
(if (isEmpty) that.toStream
else new Stream.Cons(head, (tail ++ that).asInstanceOf[Stream[A]])).asInstanceOf[That]
@@ -129,12 +129,6 @@ self =>
else new Stream.Cons(z, tail.scanLeft(op(z, head))(op).asInstanceOf[Stream[B]])).asInstanceOf[That]
}
- /** Create a new stream which contains all elements of this stream
- * followed by all elements of Iterator `that'
- */
- override def++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[Stream[A], B, That]): That =
- this ++ that.toStream
-
/** Returns the stream resulting from applying the given function
* <code>f</code> to each element of this stream.
*
@@ -435,7 +429,7 @@ object Stream extends SeqFactory[Stream] {
* this builder should be bypassed.
*/
class StreamBuilder[A] extends scala.collection.mutable.LazyBuilder[A, Stream[A]] {
- def result: Stream[A] = (for (xs <- parts.iterator; x <- xs.toIterable.iterator) yield x).toStream
+ def result: Stream[A] = parts.toStream flatMap (_.toStream)
}
object Empty extends Stream[Nothing] {
diff --git a/src/library/scala/collection/interfaces/MapMethods.scala b/src/library/scala/collection/interfaces/MapMethods.scala
index 17c23eb4e6..fd6e7ad2a7 100644
--- a/src/library/scala/collection/interfaces/MapMethods.scala
+++ b/src/library/scala/collection/interfaces/MapMethods.scala
@@ -40,6 +40,5 @@ with SubtractableMethods[A, This]
def mapValues[C](f: B => C): DefaultMap[A, C]
def updated [B1 >: B](key: A, value: B1): Map[A, B1]
def + [B1 >: B] (elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *): Map[A, B1]
- def ++[B1 >: B](elems: Traversable[(A, B1)]): Map[A, B1]
- def ++[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1]
+ def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1]
}
diff --git a/src/library/scala/collection/interfaces/SetMethods.scala b/src/library/scala/collection/interfaces/SetMethods.scala
index 8a3142b44a..453143b790 100644
--- a/src/library/scala/collection/interfaces/SetMethods.scala
+++ b/src/library/scala/collection/interfaces/SetMethods.scala
@@ -21,8 +21,7 @@ trait AddableMethods[A, +This <: Addable[A, This]] {
protected def repr: This
def +(elem: A): This
def + (elem1: A, elem2: A, elems: A*): This
- def ++ (elems: Traversable[A]): This
- def ++ (iter: Iterator[A]): This
+ def ++ (xs: TraversableOnce[A]): This
}
/**
@@ -32,8 +31,7 @@ trait SubtractableMethods[A, +This <: Subtractable[A, This]] {
protected def repr: This
def -(elem: A): This
def -(elem1: A, elem2: A, elems: A*): This
- def --(elems: Traversable[A]): This
- def --(iter: Iterator[A]): This
+ def --(xs: TraversableOnce[A]): This
}
/**
diff --git a/src/library/scala/collection/interfaces/TraversableMethods.scala b/src/library/scala/collection/interfaces/TraversableMethods.scala
index c695e2c1ef..1fc2451ec0 100644
--- a/src/library/scala/collection/interfaces/TraversableMethods.scala
+++ b/src/library/scala/collection/interfaces/TraversableMethods.scala
@@ -27,8 +27,7 @@ trait TraversableMethods[+A, +This <: TraversableLike[A, This] with Traversable[
def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[This, B, That]): That
// new collections
- def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[This, B, That]): That
- def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[This, B, That]): That
+ def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[This, B, That]): That
def copyToArray[B >: A](xs: Array[B], start: Int): Unit
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit
def copyToBuffer[B >: A](dest: Buffer[B]): Unit
diff --git a/src/library/scala/collection/interfaces/TraversableOnceMethods.scala b/src/library/scala/collection/interfaces/TraversableOnceMethods.scala
new file mode 100644
index 0000000000..1e71215efd
--- /dev/null
+++ b/src/library/scala/collection/interfaces/TraversableOnceMethods.scala
@@ -0,0 +1,69 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.collection
+package interfaces
+
+import mutable.Buffer
+
+trait TraversableOnceMethods[+A] {
+ self: TraversableOnce[A] =>
+
+ def foreach[U](f: A => U): Unit
+ protected[this] def reversed: TraversableOnce[A]
+
+ // tests
+ def isEmpty: Boolean
+ def nonEmpty: Boolean
+ def hasDefiniteSize: Boolean
+ def isTraversableAgain: Boolean
+
+ // applying a predicate
+ def forall(p: A => Boolean): Boolean
+ def exists(p: A => Boolean): Boolean
+ def find(p: A => Boolean): Option[A]
+ def count(p: A => Boolean): Int
+
+ // folds
+ def /:[B](z: B)(op: (B, A) => B): B
+ def :\[B](z: B)(op: (A, B) => B): B
+ def foldLeft[B](z: B)(op: (B, A) => B): B
+ def foldRight[B](z: B)(op: (A, B) => B): B
+ def reduceLeft[B >: A](op: (B, A) => B): B
+ def reduceRight[B >: A](op: (A, B) => B): B
+ def reduceLeftOption[B >: A](op: (B, A) => B): Option[B]
+ def reduceRightOption[B >: A](op: (A, B) => B): Option[B]
+
+ def sum[B >: A](implicit num: Numeric[B]): B
+ def product[B >: A](implicit num: Numeric[B]): B
+ def min[B >: A](implicit cmp: Ordering[B]): A
+ def max[B >: A](implicit cmp: Ordering[B]): A
+
+ // copies and conversions
+ def copyToBuffer[B >: A](dest: Buffer[B]): Unit
+ def copyToArray[B >: A](xs: Array[B], start: Int, len: Int): Unit
+ def copyToArray[B >: A](xs: Array[B], start: Int): Unit
+ def copyToArray[B >: A](xs: Array[B]): Unit
+
+ def toArray[B >: A : ClassManifest]: Array[B]
+ def toIterable: Iterable[A]
+ def toIterator: Iterator[A]
+ def toList: List[A]
+ def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U]
+ def toSet[B >: A]: immutable.Set[B]
+ def toStream: Stream[A]
+ def toTraversable: Traversable[A]
+
+ def mkString(start: String, sep: String, end: String): String
+ def mkString(sep: String): String
+ def mkString: String
+
+ def addString(buf: StringBuilder, start: String, sep: String, end: String): StringBuilder
+ def addString(buf: StringBuilder, sep: String): StringBuilder
+ def addString(buf: StringBuilder): StringBuilder
+}
diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala
index 23d691cbf1..0a46fc8a5d 100644
--- a/src/library/scala/collection/mutable/ArrayBuffer.scala
+++ b/src/library/scala/collection/mutable/ArrayBuffer.scala
@@ -68,7 +68,7 @@ class ArrayBuffer[A](override protected val initialSize: Int)
* @param iter the iterfable object.
* @return the updated buffer.
*/
- override def ++=(iter: Traversable[A]): this.type = iter match {
+ override def ++=(xs: TraversableOnce[A]): this.type = xs match {
case v: IndexedSeq[_] =>
val n = v.length
ensureSize(size0 + n)
@@ -76,7 +76,7 @@ class ArrayBuffer[A](override protected val initialSize: Int)
size0 += n
this
case _ =>
- super.++=(iter)
+ super.++=(xs)
}
/** Prepends a single element to this buffer and return
@@ -101,7 +101,7 @@ class ArrayBuffer[A](override protected val initialSize: Int)
* @param iter the iterable object.
* @return the updated buffer.
*/
- override def ++=:(iter: Traversable[A]): this.type = { insertAll(0, iter); this }
+ override def ++=:(xs: TraversableOnce[A]): this.type = { insertAll(0, xs.toTraversable); this }
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a
diff --git a/src/library/scala/collection/mutable/ArrayBuilder.scala b/src/library/scala/collection/mutable/ArrayBuilder.scala
index c824cc15f5..7e4bab353b 100644
--- a/src/library/scala/collection/mutable/ArrayBuilder.scala
+++ b/src/library/scala/collection/mutable/ArrayBuilder.scala
@@ -67,7 +67,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[T]): this.type = (xs: AnyRef) match {
+ override def ++=(xs: TraversableOnce[T]): this.type = (xs: AnyRef) match {
case xs: WrappedArray.ofRef[_] =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -131,7 +131,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Byte]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Byte]): this.type = xs match {
case xs: WrappedArray.ofByte =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -195,7 +195,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Short]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Short]): this.type = xs match {
case xs: WrappedArray.ofShort =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -259,7 +259,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Char]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Char]): this.type = xs match {
case xs: WrappedArray.ofChar =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -323,7 +323,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Int]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Int]): this.type = xs match {
case xs: WrappedArray.ofInt =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -387,7 +387,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Long]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Long]): this.type = xs match {
case xs: WrappedArray.ofLong =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -451,7 +451,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Float]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Float]): this.type = xs match {
case xs: WrappedArray.ofFloat =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -515,7 +515,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Double]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Double]): this.type = xs match {
case xs: WrappedArray.ofDouble =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -579,7 +579,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Boolean]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Boolean]): this.type = xs match {
case xs: WrappedArray.ofBoolean =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
@@ -643,7 +643,7 @@ object ArrayBuilder {
this
}
- override def ++=(xs: scala.collection.Traversable[Unit]): this.type = xs match {
+ override def ++=(xs: TraversableOnce[Unit]): this.type = xs match {
case xs: WrappedArray.ofUnit =>
ensureSize(this.size + xs.length)
Array.copy(xs.array, 0, elems, this.size, xs.length)
diff --git a/src/library/scala/collection/mutable/ArrayStack.scala b/src/library/scala/collection/mutable/ArrayStack.scala
index 1c3bdacaa5..8f9d1bfc88 100644
--- a/src/library/scala/collection/mutable/ArrayStack.scala
+++ b/src/library/scala/collection/mutable/ArrayStack.scala
@@ -104,15 +104,7 @@ class ArrayStack[T] private(private var table : Array[AnyRef],
*
* @param x The source of elements to push
*/
- def ++=(x: scala.collection.Iterable[T]): this.type = { x.foreach(this +=(_)); this }
-
-
- /**
- * Pushes all the provided elements onto the stack.
- *
- * @param x The source of elements to push
- */
- def ++=(x: Iterator[T]): this.type = { x.foreach(this +=(_)); this }
+ def ++=(xs: TraversableOnce[T]): this.type = { xs foreach += ; this }
/**
* Alias for push.
diff --git a/src/library/scala/collection/mutable/BufferLike.scala b/src/library/scala/collection/mutable/BufferLike.scala
index 5797bf0c55..0fb34cc8e0 100644
--- a/src/library/scala/collection/mutable/BufferLike.scala
+++ b/src/library/scala/collection/mutable/BufferLike.scala
@@ -131,50 +131,33 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]]
this
}
- /** Prepends the elements contained in a traversable collection
- * to this buffer.
- * @param elems the collection containing the elements to prepend.
- * @return the buffer itself.
- */
- def ++=:(elems: Traversable[A]): this.type = { insertAll(0, elems); this }
-
- /** Prepends the elements produced by an iterator to this buffer.
+ /** Prepends elements to this buffer.
*
- * @param iter the iterator producing the elements to prepend.
- * @return the buffer itself.
+ * @param xs the TraversableOnce containing the elements to prepend.
+ * @return the buffer itself.
*/
- def ++=:(iter: Iterator[A]): this.type = { insertAll(0, iter.toSeq); this }
+ def ++=:(xs: TraversableOnce[A]): this.type = { insertAll(0, xs.toTraversable); this }
/** Appends the given elements to this buffer.
*
* @param elems the elements to append.
*/
- def append(elems: A*) { this ++= elems }
+ def append(elems: A*) { appendAll(elems) }
/** Appends the elements contained in a traversable collection to this buffer.
* @param elems the collection containing the elements to append.
*/
- def appendAll(elems: Traversable[A]) { this ++= elems }
-
- /** Appends the elements produced by an iterator to this buffer.
- * @param elems the iterator producing the elements to append.
- */
- def appendAll(iter: Iterator[A]) { this ++= iter }
+ def appendAll(xs: TraversableOnce[A]) { this ++= xs }
/** Prepends given elements to this buffer.
* @param elems the elements to prepend.
*/
- def prepend(elems: A*) { elems ++=: this }
+ def prepend(elems: A*) { prependAll(elems) }
/** Prepends the elements contained in a traversable collection to this buffer.
* @param elems the collection containing the elements to prepend.
*/
- def prependAll(iter: Traversable[A]) { iter ++=: this }
-
- /** Prepends a number of elements produced by an iterator to this buffer.
- * @param iter the iterator producing the elements to prepend.
- */
- def prependAll(iter: Iterator[A]) { iter ++=: this }
+ def prependAll(xs: TraversableOnce[A]) { xs ++=: this }
/** Inserts new elements at a given index into this buffer.
*
@@ -295,18 +278,7 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]]
"As of 2.8, ++ always creates a new collection, even on Buffers.\n"+
"Use ++= instead if you intend to add by side effect to an existing collection.\n"
)
- def ++(iter: Traversable[A]): This = clone() ++= iter
-
- /** Adds a number of elements provided by an iterator and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, ++ always creates a new collection, even on Buffers.\n"+
- "Use ++= instead if you intend to add by side effect to an existing collection.\n"
- )
- def ++ (iter: Iterator[A]): This = clone() ++= iter
+ def ++(xs: TraversableOnce[A]): This = clone() ++= xs
/** Removes a single element from this collection and returns
* the collection itself.
@@ -341,16 +313,5 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]]
"As of 2.8, -- always creates a new collection, even on Buffers.\n"+
"Use --= instead if you intend to remove by side effect from an existing collection.\n"
)
- override def --(iter: Traversable[A]): This = clone() --= iter
-
- /** Removes a number of elements provided by an iterator and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, -- always creates a new collection, even on Buffers.\n"+
- "Use --= instead if you intend to remove by side effect from an existing collection.\n"
- )
- override def --(iter: Iterator[A]): This = clone() --= iter
+ override def --(xs: TraversableOnce[A]): This = clone() --= xs
}
diff --git a/src/library/scala/collection/mutable/BufferProxy.scala b/src/library/scala/collection/mutable/BufferProxy.scala
index cc8aba79ab..d4444dab67 100644
--- a/src/library/scala/collection/mutable/BufferProxy.scala
+++ b/src/library/scala/collection/mutable/BufferProxy.scala
@@ -61,14 +61,14 @@ trait BufferProxy[A] extends Buffer[A] with Proxy {
*/
@deprecated("Use ++= instead if you intend to add by side effect to an existing collection.\n"+
"Use `clone() ++=` if you intend to create a new collection.")
- def ++(iter: scala.collection.Iterable[A]): Buffer[A] = self.++(iter)
+ override def ++(xs: TraversableOnce[A]): Buffer[A] = self.++(xs)
/** Appends a number of elements provided by an iterable object
* via its <code>iterator</code> method.
*
* @param iter the iterable object.
*/
- def ++=(iter: scala.collection.Iterable[A]): this.type = { self.++=(iter); this }
+ override def ++=(xs: TraversableOnce[A]): this.type = { self.++=(xs); this }
/** Appends a sequence of elements to this buffer.
*
@@ -81,7 +81,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy {
*
* @param iter the iterable object.
*/
- def appendAll(iter: scala.collection.Iterable[A]) { self.appendAll(iter) }
+ override def appendAll(xs: TraversableOnce[A]) { self.appendAll(xs) }
/** Prepend a single element to this buffer and return
* the identity of the buffer.
@@ -90,8 +90,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy {
*/
def +=:(elem: A): this.type = { self.+=:(elem); this }
- override def ++=:(iter: scala.collection.Traversable[A]): this.type = { self.++=:(iter); this }
- override def ++=:(iter: scala.collection.Iterator[A]): this.type = { self.++=:(iter); this }
+ override def ++=:(xs: TraversableOnce[A]): this.type = { self.++=:(xs); this }
/** Prepend an element to this list.
*
@@ -105,7 +104,7 @@ trait BufferProxy[A] extends Buffer[A] with Proxy {
*
* @param iter the iterable object.
*/
- def prependAll(elems: scala.collection.Iterable[A]) { self.prependAll(elems) }
+ override def prependAll(xs: TraversableOnce[A]) { self.prependAll(xs) }
/** Inserts new elements at the index <code>n</code>. Opposed to method
* <code>update</code>, this method will not replace an element with a
diff --git a/src/library/scala/collection/mutable/Builder.scala b/src/library/scala/collection/mutable/Builder.scala
index 1706f598ec..2e6a9149bc 100644
--- a/src/library/scala/collection/mutable/Builder.scala
+++ b/src/library/scala/collection/mutable/Builder.scala
@@ -64,8 +64,7 @@ trait Builder[-Elem, +To] extends Growable[Elem] {
val self = Builder.this
def +=(x: Elem): this.type = { self += x; this }
def clear() = self.clear()
- override def ++=(xs: Iterator[Elem]): this.type = { self ++= xs; this }
- override def ++=(xs:scala.collection.Traversable[Elem]): this.type = { self ++= xs; this }
+ override def ++=(xs: TraversableOnce[Elem]): this.type = { self ++= xs; this }
def result: NewTo = f(self.result)
}
}
diff --git a/src/library/scala/collection/mutable/LazyBuilder.scala b/src/library/scala/collection/mutable/LazyBuilder.scala
index 2ce1fa9827..7714d29f08 100644
--- a/src/library/scala/collection/mutable/LazyBuilder.scala
+++ b/src/library/scala/collection/mutable/LazyBuilder.scala
@@ -20,10 +20,9 @@ import immutable.{List, Nil}
*/
abstract class LazyBuilder[Elem, +To] extends Builder[Elem, To] {
/** The different segments of elements to be added to the builder, represented as iterators */
- protected var parts = new ListBuffer[scala.collection.Traversable[Elem]]
+ protected var parts = new ListBuffer[TraversableOnce[Elem]]
def +=(x: Elem): this.type = { parts += List(x); this }
- override def ++=(xs: Iterator[Elem]): this.type = { parts += xs.toStream; this }
- override def ++=(xs: scala.collection.Traversable[Elem]): this.type = { parts += xs; this }
+ override def ++=(xs: TraversableOnce[Elem]): this.type = { parts += xs ; this }
def result(): To
def clear() { parts.clear() }
}
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala
index 09ab074783..b8e5aeb262 100644
--- a/src/library/scala/collection/mutable/ListBuffer.scala
+++ b/src/library/scala/collection/mutable/ListBuffer.scala
@@ -105,8 +105,6 @@ final class ListBuffer[A]
this
}
- override def ++=(elems: Traversable[A]): this.type = if (elems ne this) super.++=(elems) else super.++=(elems.toList)
-
/** Clears the buffer contents.
*/
def clear() {
diff --git a/src/library/scala/collection/mutable/MapLike.scala b/src/library/scala/collection/mutable/MapLike.scala
index c32ab54b7a..eca102e502 100644
--- a/src/library/scala/collection/mutable/MapLike.scala
+++ b/src/library/scala/collection/mutable/MapLike.scala
@@ -146,21 +146,8 @@ trait MapLike[A, B, +This <: MapLike[A, B, This] with Map[A, B]]
"As of 2.8, this operation creates a new map. To add the elements as a\n"+
"side effect to an existing map and return that map itself, use ++=."
)
- override def ++[B1 >: B](kvs: Traversable[(A, B1)]): Map[A, B1] =
- clone().asInstanceOf[Map[A, B1]] ++= kvs
-
- /** Adds a number of elements provided by an iterator
- * via its `iterator` method and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, this operation creates a new map. To add the elements as a\n"+
- "side effect to an existing map and return that map itself, use ++=."
- )
- override def ++[B1 >: B] (iter: Iterator[(A, B1)]): Map[A, B1] =
- clone().asInstanceOf[Map[A, B1]] ++= iter
+ override def ++[B1 >: B](xs: TraversableOnce[(A, B1)]): Map[A, B1] =
+ clone().asInstanceOf[Map[A, B1]] ++= xs
/** Removes a key from this map, returning the value associated previously
* with that key as an option.
@@ -270,16 +257,5 @@ trait MapLike[A, B, +This <: MapLike[A, B, This] with Map[A, B]]
"As of 2.8, this operation creates a new map. To remove the elements as a\n"+
"side effect to an existing map and return that map itself, use --=."
)
- override def --(kvs: Traversable[A]): This = clone() --= kvs
-
- /** Removes a number of elements provided by an iterator and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, this operation creates a new map. To remove the elements as a\n"+
- "side effect to an existing map and return that map itself, use --=."
- )
- override def --(iter: Iterator[A]): This = clone() --= iter
+ override def --(xs: TraversableOnce[A]): This = clone() --= xs
}
diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala
index 96d60ce4ea..4d74a2ee74 100644
--- a/src/library/scala/collection/mutable/PriorityQueue.scala
+++ b/src/library/scala/collection/mutable/PriorityQueue.scala
@@ -149,13 +149,7 @@ class PriorityQueue[A](implicit ord: Ordering[A])
*
* @param iter an iterable object
*/
- def ++(elems: scala.collection.Traversable[A]) = { this.clone() ++= elems }
-
- /** Adds all elements provided by an iterator into the priority queue.
- *
- * @param it an iterator
- */
- def ++(iter: Iterator[A]) = { this.clone() ++= iter }
+ def ++(xs: TraversableOnce[A]) = { this.clone() ++= xs }
/** Adds all elements to the queue.
*
@@ -230,7 +224,7 @@ class PriorityQueue[A](implicit ord: Ordering[A])
}
override def reverseIterator = new Iterator[A] {
- val arr = new Array[Any](size)
+ val arr = new Array[Any](PriorityQueue.this.size)
iterator.copyToArray(arr)
var i = arr.size - 1
def hasNext: Boolean = i >= 0
diff --git a/src/library/scala/collection/mutable/PriorityQueueProxy.scala b/src/library/scala/collection/mutable/PriorityQueueProxy.scala
index 0771ce6b60..427ffe478a 100644
--- a/src/library/scala/collection/mutable/PriorityQueueProxy.scala
+++ b/src/library/scala/collection/mutable/PriorityQueueProxy.scala
@@ -48,21 +48,11 @@ abstract class PriorityQueueProxy[A](implicit ord: Ordering[A]) extends Priority
*/
override def +=(elem: A): this.type = { self += elem; this }
- /** Adds all elements provided by an <code>Iterable</code> object
- * into the priority queue.
- *
- * @param iter an iterable object
- */
- def ++=(iter: scala.collection.Iterable[A]): this.type = {
- self ++= iter
- this
- }
-
/** Adds all elements provided by an iterator into the priority queue.
*
* @param it an iterator
*/
- override def ++=(it: Iterator[A]): this.type = {
+ override def ++=(it: TraversableOnce[A]): this.type = {
self ++= it
this
}
diff --git a/src/library/scala/collection/mutable/QueueProxy.scala b/src/library/scala/collection/mutable/QueueProxy.scala
index a322934f63..b2548b26cc 100644
--- a/src/library/scala/collection/mutable/QueueProxy.scala
+++ b/src/library/scala/collection/mutable/QueueProxy.scala
@@ -45,24 +45,13 @@ trait QueueProxy[A] extends Queue[A] with Proxy {
*/
override def +=(elem: A): this.type = { self += elem; this }
- /** Adds all elements provided by an <code>Iterable</code> object
- * at the end of the queue. The elements are prepended in the order they
- * are given out by the iterator.
- *
- * @param iter an iterable object
- */
- def ++=(iter: scala.collection.Iterable[A]): this.type = {
- self ++= iter
- this
- }
-
/** Adds all elements provided by an iterator
* at the end of the queue. The elements are prepended in the order they
* are given out by the iterator.
*
* @param iter an iterator
*/
- override def ++=(it: Iterator[A]): this.type = {
+ override def ++=(it: TraversableOnce[A]): this.type = {
self ++= it
this
}
diff --git a/src/library/scala/collection/mutable/SetLike.scala b/src/library/scala/collection/mutable/SetLike.scala
index f5a942b752..7004e52b8e 100644
--- a/src/library/scala/collection/mutable/SetLike.scala
+++ b/src/library/scala/collection/mutable/SetLike.scala
@@ -164,18 +164,7 @@ trait SetLike[A, +This <: SetLike[A, This] with Set[A]]
"As of 2.8, this operation creates a new set. To add the elements as a\n"+
"side effect to an existing set and return that set itself, use ++=."
)
- override def ++(iter: scala.collection.Traversable[A]): This = clone() ++= iter
-
- /** Adds a number of elements provided by an iterator and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, this operation creates a new set. To add the elements as a\n"+
- "side effect to an existing set and return that set itself, use ++=."
- )
- override def ++ (iter: Iterator[A]): This = clone() ++= iter
+ override def ++(xs: TraversableOnce[A]): This = clone() ++= xs
/** Removes a single element from this collection and returns
* the collection itself.
@@ -211,18 +200,7 @@ trait SetLike[A, +This <: SetLike[A, This] with Set[A]]
"As of 2.8, this operation creates a new set. To remove the elements as a\n"+
"side effect to an existing set and return that set itself, use --=."
)
- override def --(iter: scala.collection.Traversable[A]): This = clone() --= iter
-
- /** Removes a number of elements provided by an iterator and returns
- * the collection itself.
- *
- * @param iter the iterator
- */
- @migration(2, 8,
- "As of 2.8, this operation creates a new set. To remove the elements as a\n"+
- "side effect to an existing set and return that set itself, use --=."
- )
- override def --(iter: Iterator[A]): This = clone() --= iter
+ override def --(xs: TraversableOnce[A]): This = clone() --= xs
/** Send a message to this scriptable object.
*
diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala
index d26c2f3176..45e9fa24b2 100644
--- a/src/library/scala/collection/mutable/Stack.scala
+++ b/src/library/scala/collection/mutable/Stack.scala
@@ -64,24 +64,11 @@ class Stack[A] private (var elems: List[A]) extends scala.collection.Seq[A] with
* @param elems the iterator object.
* @return the stack with the new elements on top.
*/
- def pushAll(elems: Iterator[A]): this.type = { for (elem <- elems) { push(elem); () }; this }
-
- /** Push all elements provided by the given iterable object onto
- * the stack. The last element returned by the traversable object
- * will be on top of the new stack.
- *
- * @param elems the iterable object.
- * @return the stack with the new elements on top.
- */
- def pushAll(elems: scala.collection.Traversable[A]): this.type = { for (elem <- elems) { push(elem); () }; this }
-
- @deprecated("use pushAll")
- @migration(2, 8, "Stack ++= now pushes arguments on the stack from left to right.")
- def ++=(it: Iterator[A]): this.type = pushAll(it)
+ def pushAll(xs: TraversableOnce[A]): this.type = { xs foreach push ; this }
@deprecated("use pushAll")
@migration(2, 8, "Stack ++= now pushes arguments on the stack from left to right.")
- def ++=(it: scala.collection.Iterable[A]): this.type = pushAll(it)
+ def ++=(xs: TraversableOnce[A]): this.type = pushAll(xs)
/** Returns the top element of the stack. This method will not remove
* the element from the stack. An error is signaled if there is no
diff --git a/src/library/scala/collection/mutable/StackProxy.scala b/src/library/scala/collection/mutable/StackProxy.scala
index 99f556112a..d3810dd158 100644
--- a/src/library/scala/collection/mutable/StackProxy.scala
+++ b/src/library/scala/collection/mutable/StackProxy.scala
@@ -48,15 +48,7 @@ trait StackProxy[A] extends Stack[A] with Proxy {
this
}
- override def pushAll(elems: Iterator[A]): this.type = {
- self pushAll elems
- this
- }
-
- override def pushAll(elems: scala.collection.Traversable[A]): this.type = {
- self pushAll elems
- this
- }
+ override def pushAll(xs: TraversableOnce[A]): this.type = { self pushAll xs; this }
/** Pushes all elements provided by an <code>Iterable</code> object
* on top of the stack. The elements are pushed in the order they
@@ -64,21 +56,8 @@ trait StackProxy[A] extends Stack[A] with Proxy {
*
* @param iter an iterable object
*/
- @deprecated("use pushAll") override def ++=(iter: scala.collection.Iterable[A]): this.type = {
- self ++= iter
- this
- }
+ @deprecated("use pushAll") override def ++=(xs: TraversableOnce[A]): this.type = { self ++= xs ; this }
- /** Pushes all elements provided by an iterator
- * on top of the stack. The elements are pushed in the order they
- * are given out by the iterator.
- *
- * @param iter an iterator
- */
- @deprecated("use pushAll") override def ++=(it: Iterator[A]): this.type = {
- self ++= it
- this
- }
override def push(elem1: A, elem2: A, elems: A*): this.type = {
self.push(elem1).push(elem2).pushAll(elems)
diff --git a/src/library/scala/collection/mutable/SynchronizedBuffer.scala b/src/library/scala/collection/mutable/SynchronizedBuffer.scala
index f6caa57729..1c9a77c46a 100644
--- a/src/library/scala/collection/mutable/SynchronizedBuffer.scala
+++ b/src/library/scala/collection/mutable/SynchronizedBuffer.scala
@@ -60,8 +60,8 @@ trait SynchronizedBuffer[A] extends Buffer[A] {
*
* @param iter the iterable object.
*/
- override def ++(iter: Traversable[A]): Self = synchronized {
- super.++(iter)
+ override def ++(xs: TraversableOnce[A]): Self = synchronized {
+ super.++(xs)
}
/** Appends a number of elements provided by an iterable object
@@ -69,8 +69,8 @@ trait SynchronizedBuffer[A] extends Buffer[A] {
*
* @param iter the iterable object.
*/
- override def ++=(iter: Traversable[A]): this.type = synchronized[this.type] {
- super.++=(iter)
+ override def ++=(xs: TraversableOnce[A]): this.type = synchronized[this.type] {
+ super.++=(xs)
}
/** Appends a sequence of elements to this buffer.
@@ -86,8 +86,8 @@ trait SynchronizedBuffer[A] extends Buffer[A] {
*
* @param iter the iterable object.
*/
- override def appendAll(iter: Traversable[A]): Unit = synchronized {
- super.appendAll(iter)
+ override def appendAll(xs: TraversableOnce[A]): Unit = synchronized {
+ super.appendAll(xs)
}
/** Prepend a single element to this buffer and return
@@ -105,17 +105,13 @@ trait SynchronizedBuffer[A] extends Buffer[A] {
*
* @param iter the iterable object.
*/
- override def ++=:(iter: Traversable[A]): this.type = synchronized[this.type] {
- super.++=:(iter)
- }
+ override def ++=:(xs: TraversableOnce[A]): this.type = synchronized[this.type] { super.++=:(xs) }
/** Prepend an element to this list.
*
* @param elem the element to prepend.
*/
- override def prepend(elems: A*): Unit = synchronized {
- super.prependAll(elems)
- }
+ override def prepend(elems: A*): Unit = prependAll(elems)
/** Prepends a number of elements provided by an iterable object
* via its <code>iterator</code> method. The identity of the
@@ -123,8 +119,8 @@ trait SynchronizedBuffer[A] extends Buffer[A] {
*
* @param iter the iterable object.
*/
- override def prependAll(elems: Traversable[A]): Unit = synchronized {
- super.prependAll(elems)
+ override def prependAll(xs: TraversableOnce[A]): Unit = synchronized {
+ super.prependAll(xs)
}
/** Inserts new elements at the index <code>n</code>. Opposed to method
diff --git a/src/library/scala/collection/mutable/SynchronizedPriorityQueue.scala b/src/library/scala/collection/mutable/SynchronizedPriorityQueue.scala
index 066c96a651..61cb0a1331 100644
--- a/src/library/scala/collection/mutable/SynchronizedPriorityQueue.scala
+++ b/src/library/scala/collection/mutable/SynchronizedPriorityQueue.scala
@@ -39,23 +39,11 @@ class SynchronizedPriorityQueue[A](implicit ord: Ordering[A]) extends PriorityQu
this
}
- /** Adds all elements provided by an <code>Iterable</code> object
- * into the priority queue.
- *
- * @param iter an iterable object
- */
- def ++=(iter: scala.collection.Iterable[A]): this.type = {
- synchronized {
- super.++=(iter)
- }
- this
- }
-
/** Adds all elements provided by an iterator into the priority queue.
*
* @param it an iterator
*/
- override def ++=(it: Iterator[A]): this.type = {
+ override def ++=(it: TraversableOnce[A]): this.type = {
synchronized {
super.++=(it)
}
diff --git a/src/library/scala/collection/mutable/SynchronizedQueue.scala b/src/library/scala/collection/mutable/SynchronizedQueue.scala
index 3a1bc2e383..e7630cee06 100644
--- a/src/library/scala/collection/mutable/SynchronizedQueue.scala
+++ b/src/library/scala/collection/mutable/SynchronizedQueue.scala
@@ -42,15 +42,7 @@ class SynchronizedQueue[A] extends Queue[A] {
*
* @param iter an iterable object
*/
- override def ++=(iter: Traversable[A]): this.type = synchronized[this.type] { super.++=(iter) }
-
- /** Adds all elements provided by an iterator
- * at the end of the queue. The elements are prepended in the order they
- * are given out by the iterator.
- *
- * @param it an iterator
- */
- override def ++=(it: Iterator[A]): this.type = synchronized[this.type] { super.++=(it) }
+ override def ++=(xs: TraversableOnce[A]): this.type = synchronized[this.type] { super.++=(xs) }
/** Adds all elements to the queue.
*
diff --git a/src/library/scala/collection/mutable/SynchronizedSet.scala b/src/library/scala/collection/mutable/SynchronizedSet.scala
index 0f129ad2cc..d3023b9136 100644
--- a/src/library/scala/collection/mutable/SynchronizedSet.scala
+++ b/src/library/scala/collection/mutable/SynchronizedSet.scala
@@ -39,24 +39,16 @@ trait SynchronizedSet[A] extends Set[A] {
super.+=(elem)
}
- override def ++=(that: Traversable[A]): this.type = synchronized[this.type] {
- super.++=(that)
- }
-
- override def ++=(it: Iterator[A]): this.type = synchronized[this.type] {
- super.++=(it)
+ override def ++=(xs: TraversableOnce[A]): this.type = synchronized[this.type] {
+ super.++=(xs)
}
abstract override def -=(elem: A): this.type = synchronized[this.type] {
super.-=(elem)
}
- override def --=(that: Traversable[A]): this.type = synchronized[this.type] {
- super.--=(that)
- }
-
- override def --=(it: Iterator[A]): this.type = synchronized[this.type] {
- super.--=(it)
+ override def --=(xs: TraversableOnce[A]): this.type = synchronized[this.type] {
+ super.--=(xs)
}
override def update(elem: A, included: Boolean): Unit = synchronized {
diff --git a/src/library/scala/collection/mutable/SynchronizedStack.scala b/src/library/scala/collection/mutable/SynchronizedStack.scala
index ff2f986244..4394d307eb 100644
--- a/src/library/scala/collection/mutable/SynchronizedStack.scala
+++ b/src/library/scala/collection/mutable/SynchronizedStack.scala
@@ -44,21 +44,13 @@ class SynchronizedStack[A] extends Stack[A] {
*/
override def push(elem1: A, elem2: A, elems: A*): this.type = synchronized[this.type] { super.push(elem1, elem2, elems: _*) }
- /** Pushes all elements provided by an <code>Traversable</code> object
- * on top of the stack. The elements are pushed in the order they
- * are given out by the iterator.
- *
- * @param iter an iterable object
- */
- override def pushAll(elems: scala.collection.Traversable[A]): this.type = synchronized[this.type] { super.pushAll(elems) }
-
/** Pushes all elements provided by an iterator
* on top of the stack. The elements are pushed in the order they
* are given out by the iterator.
*
* @param elems an iterator
*/
- override def pushAll(elems: Iterator[A]): this.type = synchronized[this.type] { super.pushAll(elems) }
+ override def pushAll(xs: TraversableOnce[A]): this.type = synchronized[this.type] { super.pushAll(elems) }
/** Returns the top element of the stack. This method will not remove
* the element from the stack. An error is signaled if there is no
diff --git a/src/library/scala/package.scala b/src/library/scala/package.scala
index 9fa09e3b72..9f31623bdf 100644
--- a/src/library/scala/package.scala
+++ b/src/library/scala/package.scala
@@ -26,6 +26,8 @@ package object scala {
type NumberFormatException = java.lang.NumberFormatException
type AbstractMethodError = java.lang.AbstractMethodError
+ type TraversableOnce[+A] = scala.collection.TraversableOnce[A]
+
type Traversable[+A] = scala.collection.Traversable[A]
val Traversable = scala.collection.Traversable
diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala
index 3baa7e33e3..ffa248d638 100644
--- a/src/library/scala/util/Random.scala
+++ b/src/library/scala/util/Random.scala
@@ -107,19 +107,17 @@ class Random(val self: java.util.Random) {
*
* @since 2.8
*/
-object Random extends Random
-{
- import collection.Traversable
+object Random extends Random {
import collection.mutable.ArrayBuffer
import collection.generic.CanBuildFrom
/** Returns a new collection of the same type in a randomly chosen order.
*
- * @param coll the Traversable to shuffle
- * @return the shuffled Traversable
+ * @param coll the TraversableOnce to shuffle
+ * @return the shuffled TraversableOnce
*/
- def shuffle[T, CC[X] <: Traversable[X]](coll: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T] = {
- val buf = new ArrayBuffer[T] ++= coll
+ def shuffle[T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T] = {
+ val buf = new ArrayBuffer[T] ++= xs
def swap(i1: Int, i2: Int) {
val tmp = buf(i1)
@@ -132,6 +130,6 @@ object Random extends Random
swap(n - 1, k)
}
- bf(coll) ++= buf result
+ bf(xs) ++= buf result
}
}
diff --git a/test/files/lib/scalacheck-1.7-SNAPSHOT.jar.desired.sha1 b/test/files/lib/scalacheck-1.7-SNAPSHOT.jar.desired.sha1
index 79078c0d8b..7e2fa52b0c 100644
--- a/test/files/lib/scalacheck-1.7-SNAPSHOT.jar.desired.sha1
+++ b/test/files/lib/scalacheck-1.7-SNAPSHOT.jar.desired.sha1
@@ -1 +1 @@
-9b93490d8c081691f328fa3aa03946ea4cfe6697 ?scalacheck-1.7-SNAPSHOT.jar
+4c76385b1a9cb7cd619739776b940d98c4aadc6d ?scalacheck-1.7-SNAPSHOT.jar
diff --git a/test/files/pos/spec-List.scala b/test/files/pos/spec-List.scala
index 9535eaa32a..6a506aa4be 100644
--- a/test/files/pos/spec-List.scala
+++ b/test/files/pos/spec-List.scala
@@ -144,18 +144,12 @@ sealed trait List[@specialized +A] extends LinearSeq[A]
/** Create a new list which contains all elements of this list
* followed by all elements of Traversable `that'
*/
- override def ++[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[List[A], B, That]): That = {
+ override def ++[B >: A, That](xs: TraversableOnce[B])(implicit bf: CanBuildFrom[List[A], B, That]): That = {
val b = bf(this)
- if (b.isInstanceOf[ListBuffer[_]]) (this ::: that.toList).asInstanceOf[That]
- else super.++(that)
+ if (b.isInstanceOf[ListBuffer[_]]) (this ::: xs.toList).asInstanceOf[That]
+ else super.++(xs)
}
- /** Create a new list which contains all elements of this list
- * followed by all elements of Iterator `that'
- */
- override def ++[B >: A, That](that: Iterator[B])(implicit bf: CanBuildFrom[List[A], B, That]): That =
- this ++ that.toList
-
/** Overrides the method in Iterable for efficiency.
*
* @return the list itself