import mutable.{ Builder, ListBuffer }
import annotation.{tailrec, migration, bridge}
import annotation.unchecked.{ uncheckedVariance => uV }
import parallel.ParIterable
trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr]
with FilterMonadic[A, Repr]
with GenTraversableLike[A, Repr]
with Parallelizable[A, ParIterable[A]]
import Traversable.breaks._
protected type Self = Repr
def repr: Repr = this.asInstanceOf[Repr]
protected[this] def thisCollection: Traversable[A] = this.asInstanceOf[Traversable[A]]
protected[this] def toCollection(repr: Repr): Traversable[A] = repr.asInstanceOf[Traversable[A]]
protected[this] def newBuilder: Builder[A, Repr]
protected[this] def parCombiner = ParIterable.newCombiner[A]
def foreach[U](f: A => U): Unit
def hasDefiniteSize = true
def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.seq.size)
def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
++(that: GenTraversableOnce[B])(bf)
def ++:[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.size)
def ++:[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (x <- this) b += f(x)
def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (x <- this) b ++= f(x).seq
def filter(p: A => Boolean): Repr = {
def filterNot(p: A => Boolean): Repr = filter(!p(_))
def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
def partition(p: A => Boolean): (Repr, Repr) = {
for (x <- this) (if (p(x)) l else r) += x
def groupBy[K](f: A => K): immutable.Map[K, Repr] = {
val m = mutable.Map.empty[K, Builder[A, Repr]]
val bldr = m.getOrElseUpdate(key, newBuilder)
val b = immutable.Map.newBuilder[K, Repr]
def forall(p: A => Boolean): Boolean = {
if (!p(x)) { result = false; break }
def exists(p: A => Boolean): Boolean = {
if (p(x)) { result = true; break }
def find(p: A => Boolean): Option[A] = {
var result: Option[A] = None
if (p(x)) { result = Some(x); break }
def scan[B >: A, That](z: B)(op: (B, B) => B)(implicit cbf: CanBuildFrom[Repr, B, That]): That = scanLeft(z)(op)
def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (x <- this) { acc = op(acc, x); b += acc }
"This scanRight definition has changed in 2.9.\n" +
"The previous behavior can be reproduced with scanRight.reverse."
def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (elem <- scanned) b += elem
var result: () => A = () => throw new NoSuchElementException
def headOption: Option[A] = if (isEmpty) None else Some(head)
override def tail: Repr = {
if (isEmpty) throw new UnsupportedOperationException("empty.tail")
def lastOption: Option[A] = if (isEmpty) None else Some(last)
if (isEmpty) throw new UnsupportedOperationException("empty.init")
def take(n: Int): Repr = slice(0, n)
b ++= thisCollection result
else sliceWithKnownDelta(n, Int.MaxValue, -n)
def slice(from: Int, until: Int): Repr = sliceWithKnownBound(math.max(from, 0), until)
private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = {
private[scala] def sliceWithKnownDelta(from: Int, until: Int, delta: Int): Repr = {
if (until <= from) b.result
sliceInternal(from, until, b)
private[scala] def sliceWithKnownBound(from: Int, until: Int): Repr = {
if (until <= from) b.result
b.sizeHintBounded(until - from, this)
sliceInternal(from, until, b)
def takeWhile(p: A => Boolean): Repr = {
def dropWhile(p: A => Boolean): Repr = {
def span(p: A => Boolean): (Repr, Repr) = {
(if (toLeft) l else r) += x
def splitAt(n: Int): (Repr, Repr) = {
l.sizeHintBounded(n, this)
if (n >= 0) r.sizeHint(this, -n)
(if (i < n) l else r) += x
def tails: Iterator[Repr] = iterateUntilEmpty(_.tail)
def inits: Iterator[Repr] = iterateUntilEmpty(_.init)
def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) {
val end = (start + len) min xs.length
def toTraversable: Traversable[A] = thisCollection
def toIterator: Iterator[A] = toStream.iterator
def toStream: Stream[A] = toBuffer.toStream
override def toString = mkString(stringPrefix + "(", ", ", ")")
def stringPrefix : String = {
var string = repr.asInstanceOf[AnyRef].getClass.getName
val idx1 = string.lastIndexOf('.' : Int)
if (idx1 != -1) string = string.substring(idx1 + 1)
val idx2 = string.indexOf('$')
if (idx2 != -1) string = string.substring(0, idx2)
def view = new TraversableView[A, Repr] {
protected lazy val underlying = self.repr
override def foreach[U](f: A => U) = self foreach f
def view(from: Int, until: Int): TraversableView[A, Repr] = view.slice(from, until)
def withFilter(p: A => Boolean): FilterMonadic[A, Repr] = new WithFilter(p)
class WithFilter(p: A => Boolean) extends FilterMonadic[A, Repr] {
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
def foreach[U](f: A => U): Unit =
def withFilter(q: A => Boolean): WithFilter =
new WithFilter(x => p(x) && q(x))
private def iterateUntilEmpty(f: Traversable[A @uV] => Traversable[A @uV]): Iterator[Repr] = {
val it = Iterator.iterate(thisCollection)(f) takeWhile (x => !x.isEmpty)
it ++ Iterator(Nil) map (newBuilder ++= _ result)