summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/TraversableLike.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/library/scala/collection/TraversableLike.scala')
-rw-r--r--src/library/scala/collection/TraversableLike.scala44
1 files changed, 25 insertions, 19 deletions
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala
index 8b430ca3d2..32d31f0be8 100644
--- a/src/library/scala/collection/TraversableLike.scala
+++ b/src/library/scala/collection/TraversableLike.scala
@@ -6,7 +6,8 @@
** |/ **
\* */
-package scala.collection
+package scala
+package collection
import generic._
import mutable.{ Builder }
@@ -86,7 +87,7 @@ trait TraversableLike[+A, +Repr] extends Any
def repr: Repr = this.asInstanceOf[Repr]
final def isTraversableAgain: Boolean = true
-
+
/** The underlying collection seen as an instance of `$Coll`.
* By default this is implemented as the current collection object itself,
* but this can be overridden.
@@ -174,7 +175,7 @@ trait TraversableLike[+A, +Repr] extends Any
*
* @usecase def ++:[B](that: TraversableOnce[B]): $Coll[B]
* @inheritdoc
- *
+ *
* Example:
* {{{
* scala> val x = List(1)
@@ -252,18 +253,21 @@ trait TraversableLike[+A, +Repr] extends Any
b.result
}
+ private def filterImpl(p: A => Boolean, isFlipped: Boolean): Repr = {
+ val b = newBuilder
+ for (x <- this)
+ if (p(x) != isFlipped) b += x
+
+ b.result
+ }
+
/** Selects all elements of this $coll which satisfy a predicate.
*
* @param p the predicate used to test elements.
* @return a new $coll consisting of all elements of this $coll that satisfy the given
* predicate `p`. The order of the elements is preserved.
*/
- def filter(p: A => Boolean): Repr = {
- val b = newBuilder
- for (x <- this)
- if (p(x)) b += x
- b.result
- }
+ def filter(p: A => Boolean): Repr = filterImpl(p, isFlipped = false)
/** Selects all elements of this $coll which do not satisfy a predicate.
*
@@ -271,11 +275,11 @@ trait TraversableLike[+A, +Repr] extends Any
* @return a new $coll consisting of all elements of this $coll that do not satisfy the given
* predicate `p`. The order of the elements is preserved.
*/
- def filterNot(p: A => Boolean): Repr = filter(!p(_))
+ def filterNot(p: A => Boolean): Repr = filterImpl(p, isFlipped = true)
def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
val b = bf(repr)
- for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
+ foreach(pf.runWith(b += _))
b.result
}
@@ -341,8 +345,8 @@ trait TraversableLike[+A, +Repr] extends Any
* $mayNotTerminateInf
*
* @param p the predicate used to test elements.
- * @return `true` if the given predicate `p` holds for all elements
- * of this $coll, otherwise `false`.
+ * @return `true` if this $coll is empty, otherwise `true` if the given predicate `p`
+ * holds for all elements of this $coll, otherwise `false`.
*/
def forall(p: A => Boolean): Boolean = {
var result = true
@@ -358,8 +362,8 @@ trait TraversableLike[+A, +Repr] extends Any
* $mayNotTerminateInf
*
* @param p the predicate used to test elements.
- * @return `true` if the given predicate `p` holds for some of the
- * elements of this $coll, otherwise `false`.
+ * @return `false` if this $coll is empty, otherwise `true` if the given predicate `p`
+ * holds for some of the elements of this $coll, otherwise `false`
*/
def exists(p: A => Boolean): Boolean = {
var result = false
@@ -415,7 +419,7 @@ trait TraversableLike[+A, +Repr] extends Any
/** Selects the first element of this $coll.
* $orderDependent
* @return the first element of this $coll.
- * @throws `NoSuchElementException` if the $coll is empty.
+ * @throws NoSuchElementException if the $coll is empty.
*/
def head: A = {
var result: () => A = () => throw new NoSuchElementException
@@ -469,7 +473,7 @@ trait TraversableLike[+A, +Repr] extends Any
* $orderDependent
* @return a $coll consisting of all elements of this $coll
* except the last one.
- * @throws `UnsupportedOperationException` if the $coll is empty.
+ * @throws UnsupportedOperationException if the $coll is empty.
*/
def init: Repr = {
if (isEmpty) throw new UnsupportedOperationException("empty.init")
@@ -477,7 +481,7 @@ trait TraversableLike[+A, +Repr] extends Any
var follow = false
val b = newBuilder
b.sizeHint(this, -1)
- for (x <- this.seq) {
+ for (x <- this) {
if (follow) b += lst
else follow = true
lst = x
@@ -502,7 +506,7 @@ trait TraversableLike[+A, +Repr] extends Any
private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = {
var i = 0
breakable {
- for (x <- this.seq) {
+ for (x <- this) {
if (i >= from) b += x
i += 1
if (i >= until) break
@@ -619,7 +623,9 @@ trait TraversableLike[+A, +Repr] extends Any
}
}
+ @deprecatedOverriding("Enforce contract of toTraversable that if it is Traversable it returns itself.", "2.11.0")
def toTraversable: Traversable[A] = thisCollection
+
def toIterator: Iterator[A] = toStream.iterator
def toStream: Stream[A] = toBuffer.toStream
// Override to provide size hint.