CMIContent Marketing InstituteContent directory

Marketing · Social Media · Instagram

CodeMirror: Scala mode

Instagram Home Marketing Social Media

CodeMirror

Home Manual Code

Language modes Scala

Scala mode

/* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */

package scala.collection

import generic._ 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]`. * * $traversableInfo * @define mutability * @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 * * `scala.collection.immutable.Stream` . * 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`. * * @author Martin Odersky * @version 2.8 * @since 2.8 * @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 TraversableOnce[A] with GenTraversableLike[A, Repr] with Parallelizable[A, ParIterable[A]] { self =>

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. */ def isEmpty: Boolean = { var result = true breakable { for (x : A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.seq.size) b ++= thisCollection b ++= that.seq b.result }

@bridge 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. * @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 ++:[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 = { val b = bf(repr) if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.size) b ++= that b ++= thisCollection b.result }

/** 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 = (that ++ seq)(breakOut)

def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) b.sizeHint(this) for (x GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) for (x Boolean): Repr = { val b = newBuilder for (x Boolean): Repr = filter(!p(_))

def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) for (x 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 = { val b = bf(repr) for (x b += y case _ => } b.result } */

/** 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) = { val l, r = newBuilder for (x K): immutable.Map[K, Repr] = { val m = mutable.Map.empty[K, Builder[A, Repr]] for (elem Boolean): Boolean = { var result = true breakable { for (x Boolean): Boolean = { var result = false breakable { for (x Boolean): Option[A] = { var result: Option[A] = None breakable { for (x : 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 = { val b = bf(repr) b.sizeHint(this, 1) var acc = z b += acc for (x B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { var scanned = List(z) var acc = z for (x A = () => throw new NoSuchElementException breakable { for (x x break } } result() }

/** Optionally selects the first element. * $orderDependent * @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. * $orderDependent * @return a $coll consisting of all elements of this $coll * except the first one. * @throws `UnsupportedOperationException` if the $coll is empty. */ override def tail: Repr = { if (isEmpty) throw new UnsupportedOperationException("empty.tail") drop(1) }

/** Selects the last element. * $orderDependent * @return The last element of this $coll. * @throws NoSuchElementException If the $coll is empty. */ def last: A = { var lst = head for (x = 0, until > 0, builder already configured for building. private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = { var i = 0 breakable { for (x = from) b += x i += 1 if (i >= until) break } } b.result } // Precondition: from >= 0 private[scala] def sliceWithKnownDelta(from: Int, until: Int, delta: Int): Repr = { val b = newBuilder if (until = 0 private[scala] def sliceWithKnownBound(from: Int, until: Int): Repr = { val b = newBuilder if (until Boolean): Repr = { val b = newBuilder breakable { for (x Boolean): Repr = { val b = newBuilder var go = false for (x Boolean): (Repr, Repr) = { val l, r = newBuilder var toLeft = true for (x = 0) r.sizeHint(this, -n) var i = 0 for (x : A](xs: Array[B], start: Int, len: Int) { var i = start val end = (start + len) min xs.length breakable { for (x = end) break xs(i) = x i += 1 } } }

def toTraversable: Traversable[A] = thisCollection def toIterator: Iterator[A] = toStream.iterator def toStream: Stream[A] = toBuffer.toStream

/** Converts this $coll to a string. * * @return a string representation of this collection. By default this * string consists of the `stringPrefix` of this $coll, * followed by all elements separated by commas and enclosed in parentheses. */ override def toString = mkString(stringPrefix + "(", ", ", ")")

/** Defines the prefix of this object's `toString` representation. * * @return a string representation which starts the result of `toString` * applied to this $coll. By default the string prefix is the * simple name of the collection class $coll. */ 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) string }

/** Creates a non-strict view of this $coll. * * @return a non-strict view of this $coll. */ def view = new TraversableView[A, Repr] { protected lazy val underlying = self.repr override def foreach[U](f: A => U) = self foreach f }

/** Creates a non-strict view of a slice of this $coll. * * Note: the difference between `view` and `slice` is that `view` produces * a view of the current $coll, whereas `slice` produces a new $coll. * * Note: `view(from, to)` is equivalent to `view.slice(from, to)` * $orderDependent * * @param from the index of the first element of the view * @param until the index of the element following the view * @return a non-strict view of a slice of this $coll, starting at index `from` * and extending up to (but not including) index `until`. */ def view(from: Int, until: Int): TraversableView[A, Repr] = view.slice(from, until)

/** Creates a non-strict filter of this $coll. * * Note: the difference between `c filter p` and `c withFilter p` is that * the former creates a new collection, whereas the latter only * restricts the domain of subsequent `map`, `flatMap`, `foreach`, * and `withFilter` operations. * $orderDependent * * @param p the predicate used to test elements. * @return an object of class `WithFilter`, which supports * `map`, `flatMap`, `foreach`, and `withFilter` operations. * All these operations apply to those elements of this $coll which * satisfy the predicate `p`. */ def withFilter(p: A => Boolean): FilterMonadic[A, Repr] = new WithFilter(p)

/** A class supporting filtered operations. Instances of this class are * returned by method `withFilter`. */ class WithFilter(p: A => Boolean) extends FilterMonadic[A, Repr] {

/** Builds a new collection by applying a function to all elements of the * outer $coll containing this `WithFilter` instance that satisfy predicate `p`. * * @param f the function to apply to each element. * @tparam B the element type of the returned collection. * @tparam That $thatinfo * @param bf $bfinfo * @return a new collection of type `That` resulting from applying * the given function `f` to each element of the outer $coll * that satisfies predicate `p` and collecting the results. * * @usecase def map[B](f: A => B): $Coll[B] * * @return a new $coll resulting from applying the given function * `f` to each element of the outer $coll that satisfies * predicate `p` and collecting the results. */ def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) for (x TraversableOnce[B]): $Coll[B] * * @return a new $coll resulting from applying the given collection-valued function * `f` to each element of the outer $coll that satisfies predicate `p` and concatenating the results. */ def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) for (x Unit): Unit */ def foreach[U](f: A => U): Unit = for (x Boolean): WithFilter = new WithFilter(x => p(x) && q(x)) }

// A helper for tails and inits. 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) } }

var editor = CodeMirror.fromTextArea(document.getElementById("code"), { lineNumbers: true, matchBrackets: true, theme: "ambiance", mode: "text/x-scala" });

More in Instagram · More in Social Media · More in Marketing