: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<title>CodeMirror: Scala mode</title>
<link rel=stylesheet href="../../doc/docs.css">
<link rel="stylesheet" href="../../lib/codemirror.css">
<link rel="stylesheet" href="../../theme/ambiance.css">
<script src="../../lib/codemirror.js"></script>
<script src="../../addon/edit/matchbrackets.js"></script>
<script src="clike.js"></script>
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
<li><a href="../../index.html">Home</a>
<li><a href="../../doc/manual.html">Manual</a>
<li><a href="https://github.com/codemirror/codemirror">Code</a>
<li><a href="../index.html">Language modes</a>
<li><a class=active href="#">Scala</a>
<textarea id="code" name="code">
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
import mutable.{ Builder, ListBuffer }
import annotation.{tailrec, migration, bridge}
import annotation.unchecked.{ uncheckedVariance => uV }
import parallel.ParIterable
/** A template trait for traversable collections of type `Traversable[A]`.
* @define traversableInfo
* This is a base trait of all kinds of $mutability Scala collections. It
* implements the behavior common to all collections, in terms of a method
* `foreach` with signature:
* def foreach[U](f: Elem => U): Unit
* Collection classes mixing in this trait provide a concrete
* `foreach` method which traverses all the
* elements contained in the collection, applying a given function to each.
* They also need to provide a method `newBuilder`
* which creates a builder for collections of the same kind.
* A traversable class might or might not have two properties: strictness
* and orderedness. Neither is represented as a type.
* The instances of a strict collection class have all their elements
* computed before they can be used as values. By contrast, instances of
* a non-strict collection class may defer computation of some of their
* elements until after the instance is available as a value.
* A typical example of a non-strict collection class is a
* <a href="../immutable/Stream.html" target="ContentFrame">
* `scala.collection.immutable.Stream`</a>.
* A more general class of examples are `TraversableViews`.
* If a collection is an instance of an ordered collection class, traversing
* its elements with `foreach` will always visit elements in the
* same order, even for different runs of the program. If the class is not
* ordered, `foreach` can visit elements in different orders for
* different runs (but it will keep the same order in the same run).'
* A typical example of a collection class which is not ordered is a
* `HashMap` of objects. The traversal order for hash maps will
* depend on the hash codes of its elements, and these hash codes might
* differ from one run to the next. By contrast, a `LinkedHashMap`
* is ordered because it's `foreach` method visits elements in the
* order they were inserted into the `HashMap`.
* @tparam A the element type of the collection
* @tparam Repr the type of the actual collection containing the elements.
* @define Coll Traversable
* @define coll traversable collection
trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr]
with FilterMonadic[A, Repr]
with GenTraversableLike[A, Repr]
with Parallelizable[A, ParIterable[A]]
import Traversable.breaks._
/** The type implementing this traversable */
protected type Self = Repr
/** The collection of type $coll underlying this `TraversableLike` object.
* By default this is implemented as the `TraversableLike` object itself,
* but this can be overridden.
def repr: Repr = this.asInstanceOf[Repr]
/** 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.
protected[this] def thisCollection: Traversable[A] = this.asInstanceOf[Traversable[A]]
/** A conversion from collections of type `Repr` to `$Coll` objects.
* By default this is implemented as just a cast, but this can be overridden.
protected[this] def toCollection(repr: Repr): Traversable[A] = repr.asInstanceOf[Traversable[A]]
/** Creates a new builder for this collection type.
protected[this] def newBuilder: Builder[A, Repr]
protected[this] def parCombiner = ParIterable.newCombiner[A]
/** Applies a function `f` to all elements of this $coll.
* Note: this method underlies the implementation of most other bulk operations.
* It's important to implement this method in an efficient way.
* @param f the function that is applied for its side-effect to every element.
* The result of function `f` is discarded.
* @tparam U the type parameter describing the result of function `f`.
* This result will always be ignored. Typically `U` is `Unit`,
* but this is not necessary.
* @usecase def foreach(f: A => Unit): Unit
def foreach[U](f: A => U): Unit
/** Tests whether this $coll is empty.
* @return `true` if the $coll contain no elements, `false` otherwise.
/** 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
* such as `Stream`, the predicate returns `true` if all elements have been computed.
* It returns `false` if the stream is not yet evaluated to the end.
* Note: many collection methods will not work on collections of infinite sizes.
* @return `true` if this collection is known to have finite size, `false` otherwise.
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)
/** Concatenates this $coll with the elements of a traversable collection.
* It differs from ++ in that the right operand determines the type of the
* resulting collection rather than the left one.
* @param that the traversable to append.
* @tparam B the element type of the returned collection.
* @return a new collection of type `That` which contains all elements
* of this $coll followed by all elements of `that`.
* @usecase def ++:[B](that: TraversableOnce[B]): $Coll[B]
* @return a new $coll which contains all elements of this $coll
* followed by all elements of `that`.
def ++:[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.size)
/** This overload exists because: for the implementation of ++: we should reuse
* that of ++ because many collections override it with more efficient versions.
* Since TraversableOnce has no '++' method, we have to implement that directly,
* but Traversable and down can use the overload.
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
/** 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 = {
/** Selects all elements of this $coll which do not satisfy a predicate.
* @param p the predicate used to test elements.
* @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 collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
/** Builds a new collection by applying an option-valued function to all
* elements of this $coll on which the function is defined.
* @param f the option-valued function which filters and maps the $coll.
* @tparam B the element type of the returned collection.
* @return a new collection of type `That` resulting from applying the option-valued function
* `f` to each element and collecting all defined results.
* The order of the elements is preserved.
* @usecase def filterMap[B](f: A => Option[B]): $Coll[B]
* @param pf the partial function which filters and maps the $coll.
* @return a new $coll resulting from applying the given option-valued function
* `f` to each element and collecting all defined results.
* The order of the elements is preserved.
def filterMap[B, That](f: A => Option[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
/** Partitions this $coll in two ${coll}s according to a predicate.
* @param p the predicate on which to partition.
* @return a pair of ${coll}s: the first $coll consists of all elements that
* satisfy the predicate `p` and the second $coll consists of all elements
* that don't. The relative order of the elements in the resulting ${coll}s
* is the same as in the original $coll.
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]
/** Tests whether a predicate holds for all elements of this $coll.
* @param p the predicate used to test elements.
* @return `true` if the given predicate `p` holds for all elements
* of this $coll, otherwise `false`.
def forall(p: A => Boolean): Boolean = {
if (!p(x)) { result = false; break }
/** Tests whether a predicate holds for some of the elements of this $coll.
* @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`.
def exists(p: A => Boolean): Boolean = {
if (p(x)) { result = true; break }
/** Finds the first element of the $coll satisfying a predicate, if any.
* @param p the predicate used to test elements.
* @return an option value containing the first element in the $coll
* that satisfies `p`, or `None` if none exists.
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
/** Selects the first element of this $coll.
* @return the first element of this $coll.
* @throws `NoSuchElementException` if the $coll is empty.
var result: () => A = () => throw new NoSuchElementException
/** Optionally selects the first element.
* @return the first element of this $coll if it is nonempty, `None` if it is empty.
def headOption: Option[A] = if (isEmpty) None else Some(head)
/** Selects all elements except the first.
* @return a $coll consisting of all elements of this $coll
* @throws `UnsupportedOperationException` if the $coll is empty.
override def tail: Repr = {
if (isEmpty) throw new UnsupportedOperationException("empty.tail")
/** Selects the last element.
* @return The last element of this $coll.
* @throws NoSuchElementException If the $coll is empty.
/** Optionally selects the last element.
* @return the last element of this $coll$ if it is nonempty, `None` if it is empty.
def lastOption: Option[A] = if (isEmpty) None else Some(last)
/** Selects all elements except the last.
* @return a $coll consisting of all elements of this $coll
* @throws `UnsupportedOperationException` if the $coll is empty.
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)
// Precondition: from >= 0, until > 0, builder already configured for building.
private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = {
// Precondition: from >= 0
private[scala] def sliceWithKnownDelta(from: Int, until: Int, delta: Int): Repr = {
if (until <= from) b.result
sliceInternal(from, until, b)
// Precondition: from >= 0