| 776 |
lars |
1 |
<!doctype html>
|
|
|
2 |
<title>CodeMirror: Scala mode</title>
|
|
|
3 |
<meta charset="utf-8" />
|
|
|
4 |
<link rel=stylesheet href="../../doc/docs.css">
|
|
|
5 |
<link rel="stylesheet" href="../../lib/codemirror.css">
|
|
|
6 |
<link rel="stylesheet" href="../../theme/ambiance.css">
|
|
|
7 |
<script src="../../lib/codemirror.js"></script>
|
|
|
8 |
<script src="../../addon/edit/matchbrackets.js"></script>
|
|
|
9 |
<script src="clike.js"></script>
|
|
|
10 |
<div id=nav>
|
|
|
11 |
<a href="http://codemirror.net">
|
|
|
12 |
<h1>CodeMirror</h1>
|
|
|
13 |
<img id=logo src="../../doc/logo.png">
|
|
|
14 |
</a>
|
|
|
15 |
<ul>
|
|
|
16 |
<li>
|
|
|
17 |
<a href="../../index.html">Home</a>
|
|
|
18 |
<li>
|
|
|
19 |
<a href="../../doc/manual.html">Manual</a>
|
|
|
20 |
<li>
|
|
|
21 |
<a href="https://github.com/codemirror/codemirror">Code</a>
|
|
|
22 |
</ul>
|
|
|
23 |
<ul>
|
|
|
24 |
<li>
|
|
|
25 |
<a href="../index.html">Language modes</a>
|
|
|
26 |
<li>
|
|
|
27 |
<a class=active href="#">Scala</a>
|
|
|
28 |
</ul>
|
|
|
29 |
</div>
|
|
|
30 |
<article>
|
|
|
31 |
<h2>Scala mode</h2>
|
|
|
32 |
<form>
|
|
|
33 |
<textarea id="code" name="code"> /* __ *\ ** ________ ___ / / ___ Scala API ** ** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ package scala.collection import generic._
|
|
|
34 |
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]`.
|
|
|
35 |
* * $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:
|
|
|
36 |
* {{{ * 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
|
|
|
37 |
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
|
|
|
38 |
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
|
|
|
39 |
as a value. * A typical example of a non-strict collection class is a *
|
|
|
40 |
<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
|
|
|
41 |
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
|
|
|
42 |
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
|
|
|
43 |
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
|
|
|
44 |
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,
|
|
|
45 |
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
|
|
|
46 |
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
|
|
|
47 |
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
|
|
|
48 |
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,
|
|
|
49 |
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
|
|
|
50 |
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
|
|
|
51 |
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
|
|
|
52 |
elements, `false` otherwise. */ def isEmpty: Boolean = { var result = true breakable { for (x
|
|
|
53 |
<- this) { result=f alse break } } result } /** Tests whether this $coll is known to have a finite size. * All strict collections are known to have
|
|
|
54 |
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
|
|
|
55 |
on collections of infinite sizes. * * @return `true` if this collection is known to have finite size, `false` otherwise. */ def hasDefiniteSize=t rue def ++[B>: 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
|
|
|
56 |
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
|
|
|
57 |
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
|
|
|
58 |
* @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
|
|
|
59 |
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)
|
|
|
60 |
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 '++'
|
|
|
61 |
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
|
|
|
62 |
=> B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) b.sizeHint(this) for (x
|
|
|
63 |
<- this) b +=f (x) b.result } def flatMap[B, That](f: A=> GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) for (x
|
|
|
64 |
<- this) b ++=f (x).seq b.result } /** Selects all elements of this $coll which satisfy a predicate. * * @param p the predicate used to
|
|
|
65 |
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
|
|
|
66 |
<- this) if (p(x)) b +=x b.result } /** 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
|
|
|
67 |
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 = { val b = bf(repr) for (x
|
|
|
68 |
<- this) if (pf.isDefinedAt(x)) b +=p f(x) b.result } /** Builds a new collection
|
|
|
69 |
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
|
|
|
70 |
collection. * @tparam That $thatinfo * @param bf $bfinfo * @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
|
|
|
71 |
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.
|
|
|
72 |
* 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
|
|
|
73 |
<- this) f(x) match { case Some(y)=> 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
|
|
|
74 |
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
|
|
|
75 |
=> Boolean): (Repr, Repr) = { val l, r = newBuilder for (x
|
|
|
76 |
<- this) (if (p(x)) l else r) +=x (l.result, r.result) } def groupBy[K](f: A=> K): immutable.Map[K, Repr] = { val m = mutable.Map.empty[K, Builder[A, Repr]] for (elem
|
|
|
77 |
<- this) { val key=f (elem) val bldr=m .getOrElseUpdate(key, newBuilder) bldr +=e lem } val b=i mmutable.Map.newBuilder[K, Repr]
|
|
|
78 |
for ((k, v) <- m) b +=( (k, v.result)) b.result } /** Tests whether a predicate holds for all elements of this $coll. * * $mayNotTerminateInf * * @param p the predicate used to test elements. * @return `true` if
|
|
|
79 |
the given predicate `p` holds for all elements * of this $coll, otherwise `false`. */ def forall(p: A=> Boolean): Boolean = { var result = true breakable { for (x
|
|
|
80 |
<- this) if (!p(x)) { result=f alse; break } } result } /** Tests whether a predicate holds for some of the elements of this $coll. * * $mayNotTerminateInf
|
|
|
81 |
* * @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 = { var result = false breakable { for (x
|
|
|
82 |
<- this) if (p(x)) { result=t rue; break } } result } /** Finds the first element of the $coll satisfying a predicate, if any. * * $mayNotTerminateInf
|
|
|
83 |
* $orderDependent * * @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=>
|
|
|
84 |
Boolean): Option[A] = { var result: Option[A] = None breakable { for (x
|
|
|
85 |
<- this) if (p(x)) { result=S ome(x); break } } result } 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 = { val
|
|
|
86 |
b = bf(repr) b.sizeHint(this, 1) var acc = z b += acc for (x
|
|
|
87 |
<- this) { acc=o p(acc, x); b +=a cc } b.result } @migration(2, 9, "This scanRight definition has changed in 2.9.\n" +
|
|
|
88 |
"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 = { var scanned = List(z) var acc = z for (x
|
|
|
89 |
<- reversed) { acc=o p(x, acc) scanned ::=a cc } val b=b f(repr) for (elem <- scanned) b +=e lem b.result
|
|
|
90 |
} /** Selects the first element of this $coll. * $orderDependent * @return the first element of this $coll. * @throws `NoSuchElementException` if the $coll is empty. */ def head: A={ var result:
|
|
|
91 |
()=> A = () => throw new NoSuchElementException breakable { for (x
|
|
|
92 |
<- this) { result=( )=> 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]
|
|
|
93 |
= 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
|
|
|
94 |
`UnsupportedOperationException` if the $coll is empty. */ override def tail: Repr = { if (isEmpty) throw new UnsupportedOperationException("empty.tail") drop(1) } /** Selects the last
|
|
|
95 |
element. * $orderDependent * @return The last element of this $coll. * @throws NoSuchElementException If the $coll is empty. */ def last: A = { var lst = head for (x
|
|
|
96 |
<- this) lst=x lst
|
|
|
97 |
} /** Optionally selects the last element. * $orderDependent * @return the last element of this $coll$ if it is nonempty, `None` if it is empty. */ def lastOption: Option[A]=i f (isEmpty)
|
|
|
98 |
None else Some(last) /** Selects all elements except the last. * $orderDependent * @return a $coll consisting of all elements of this $coll * except the last one. * @throws `UnsupportedOperationException`
|
|
|
99 |
if the $coll is empty. */ def init: Repr={ if (isEmpty) throw new UnsupportedOperationException( "empty.init") var lst=h ead var follow=f alse val b=n ewBuilder b.sizeHint(this, -1) for
|
|
|
100 |
(x <- this.seq) { if (follow) b +=l st else follow=t rue lst=x } b.result } def take(n: Int): Repr=s lice(0, n) def drop(n: Int): Repr=i f (n <=0 ) { val b=n ewBuilder b.sizeHint(this)
|
|
|
101 |
b ++=t hisCollection result } else sliceWithKnownDelta(n, Int.MaxValue, -n) def slice(from: Int, until: Int): Repr=s liceWithKnownBound(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 = { var i = 0 breakable { for (x
|
|
|
102 |
<- this.seq)
|
|
|
103 |
{ if (i>= 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
|
|
|
104 |
if (until
|
|
|
105 |
<=f rom) b.result else { b.sizeHint(this, delta) sliceInternal(from, until, b) } } // Precondition: from>= 0 private[scala] def sliceWithKnownBound(from: Int, until: Int): Repr = { val b = newBuilder if (until
|
|
|
106 |
<=f rom) b.result else { b.sizeHintBounded(until - from, this) sliceInternal(from,
|
|
|
107 |
until, b) } } def takeWhile(p: A=> Boolean): Repr = { val b = newBuilder breakable { for (x
|
|
|
108 |
<- this) { if (!p(x)) break b +=x } } b.result } def dropWhile(p: A=> Boolean): Repr = { val b = newBuilder var go = false for (x
|
|
|
109 |
<- this) { if (!p(x)) go=t rue if (go) b +=x } b.result } def span(p: A=> Boolean): (Repr, Repr) = { val l, r = newBuilder var toLeft = true for (x
|
|
|
110 |
<- this) { toLeft=t oLeft && p(x) (if (toLeft) l else r) +=x } (l.result, r.result) } def
|
|
|
111 |
splitAt(n: Int): (Repr, Repr)={ val l, r=n ewBuilder l.sizeHintBounded(n, this) if (n>= 0) r.sizeHint(this, -n) var i = 0 for (x
|
|
|
112 |
<- this) { (if (i < n) l else r) +=x i +=1 } (l.result, r.result) } /** Iterates over the tails of this $coll. The first
|
|
|
113 |
value will be this * $coll and the final one will be an empty $coll, with the intervening * values the results of successive applications of `tail`. * * @return
|
|
|
114 |
an iterator over all the tails of this $coll * @example `List(1,2,3).tails=I terator(List(1,2,3), List(2,3), List(3), Nil)` */ def tails: Iterator[Repr]=i
|
|
|
115 |
terateUntilEmpty(_.tail) /** Iterates over the inits of this $coll. The first value will be this * $coll and the final one will be an empty $coll, with the
|
|
|
116 |
intervening * values the results of successive applications of `init`. * * @return an iterator over all the inits of this $coll * @example `List(1,2,3).inits=I
|
|
|
117 |
terator(List(1,2,3), List(1,2), List(1), Nil)` */ def inits: Iterator[Repr]=i terateUntilEmpty(_.init) /** Copies elements of this $coll to an array. * Fills
|
|
|
118 |
the given array `xs` with at most `len` elements of * this $coll, starting at position `start`. * Copying will stop once either the end of the current $coll
|
|
|
119 |
is reached, * or the end of the array is reached, or `len` elements have been copied. * * $willNotTerminateInf * * @param xs the array to fill. * @param start
|
|
|
120 |
the starting index. * @param len the maximal number of elements to copy. * @tparam B the type of the elements of the array. * * * @usecase def copyToArray(xs:
|
|
|
121 |
Array[A], start: Int, len: Int): Unit */ def copyToArray[B>: A](xs: Array[B], start: Int, len: Int) { var i = start val end = (start + len) min xs.length breakable { for (x
|
|
|
122 |
<- this) { if (i>= end) break xs(i) = x i += 1 } } } def toTraversable: Traversable[A] = thisCollection def toIterator: Iterator[A] = toStream.iterator def toStream: Stream[A]
|
|
|
123 |
= toBuffer.toStream /** Converts this $coll to a string. * * @return a string representation of this collection. By default this * string consists
|
|
|
124 |
of the `stringPrefix` of this $coll, * followed by all elements separated by commas and enclosed in parentheses. */ override def toString = mkString(stringPrefix
|
|
|
125 |
+ "(", ", ", ")") /** Defines the prefix of this object's `toString` representation. * * @return a string representation which starts the result of
|
|
|
126 |
`toString` * applied to this $coll. By default the string prefix is the * simple name of the collection class $coll. */ def stringPrefix : String =
|
|
|
127 |
{ var string = repr.asInstanceOf[AnyRef].getClass.getName val idx1 = string.lastIndexOf('.' : Int) if (idx1 != -1) string = string.substring(idx1 +
|
|
|
128 |
1) val idx2 = string.indexOf('$') if (idx2 != -1) string = string.substring(0, idx2) string } /** Creates a non-strict view of this $coll. * * @return
|
|
|
129 |
a non-strict view of this $coll. */ def view = new TraversableView[A, Repr] { protected lazy val underlying = self.repr override def foreach[U](f:
|
|
|
130 |
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`
|
|
|
131 |
produces * a view of the current $coll, whereas `slice` produces a new $coll. * * Note: `view(from, to)` is equivalent to `view.slice(from, to)` *
|
|
|
132 |
$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
|
|
|
133 |
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):
|
|
|
134 |
TraversableView[A, Repr] = view.slice(from, until) /** Creates a non-strict filter of this $coll. * * Note: the difference between `c filter p` and
|
|
|
135 |
`c withFilter p` is that * the former creates a new collection, whereas the latter only * restricts the domain of subsequent `map`, `flatMap`, `foreach`,
|
|
|
136 |
* and `withFilter` operations. * $orderDependent * * @param p the predicate used to test elements. * @return an object of class `WithFilter`, which
|
|
|
137 |
supports * `map`, `flatMap`, `foreach`, and `withFilter` operations. * All these operations apply to those elements of this $coll which * satisfy the
|
|
|
138 |
predicate `p`. */ def withFilter(p: A => Boolean): FilterMonadic[A, Repr] = new WithFilter(p) /** A class supporting filtered operations. Instances
|
|
|
139 |
of this class are * returned by method `withFilter`. */ class WithFilter(p: A => Boolean) extends FilterMonadic[A, Repr] { /** Builds a new collection
|
|
|
140 |
by applying a function to all elements of the * outer $coll containing this `WithFilter` instance that satisfy predicate `p`. * * @param f the function
|
|
|
141 |
to apply to each element. * @tparam B the element type of the returned collection. * @tparam That $thatinfo * @param bf $bfinfo * @return a new collection
|
|
|
142 |
of type `That` resulting from applying * the given function `f` to each element of the outer $coll * that satisfies predicate `p` and collecting the
|
|
|
143 |
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
|
|
|
144 |
outer $coll that satisfies * predicate `p` and collecting the results. */ def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That
|
|
|
145 |
= { val b = bf(repr) for (x
|
|
|
146 |
<- self) if (p(x)) b +=f (x) b.result } /** Builds a new collection by applying a function to all elements of the * outer
|
|
|
147 |
$coll containing this `WithFilter` instance that satisfy * predicate `p` and concatenating the results. * * @param f the function to apply to each
|
|
|
148 |
element. * @tparam B the element type of the returned collection. * @tparam That $thatinfo * @param bf $bfinfo * @return a new collection of type `That`
|
|
|
149 |
resulting from applying * the given collection-valued function `f` to each element * of the outer $coll that satisfies predicate `p` and * concatenating
|
|
|
150 |
the results. * * @usecase def flatMap[B](f: A=> TraversableOnce[B]): $Coll[B] * * @return a new $coll resulting from applying the given collection-valued function * `f` to each element of the outer
|
|
|
151 |
$coll that satisfies predicate `p` and concatenating the results. */ def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr,
|
|
|
152 |
B, That]): That = { val b = bf(repr) for (x
|
|
|
153 |
<- self) if (p(x)) b ++=f (x).seq b.result } /** Applies a function `f` to all elements of the outer
|
|
|
154 |
$coll containing * this `WithFilter` instance that satisfy predicate `p`. * * @param f the function that is applied for its side-effect to every
|
|
|
155 |
element. * The result of function `f` is discarded. * * @tparam U the type parameter describing the result of function `f`. * This result will
|
|
|
156 |
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 = for (x
|
|
|
157 |
<- self) if (p(x)) f(x) /** Further refines the filter for this $coll. * * @param q the
|
|
|
158 |
predicate used to test elements. * @return an object of class `WithFilter`, which supports * `map`, `flatMap`, `foreach`, and `withFilter`
|
|
|
159 |
operations. * All these operations apply to those elements of this $coll which * satisfy the predicate `q` in addition to the predicate `p`.
|
|
|
160 |
*/ def withFilter(q: A=> Boolean): WithFilter = new WithFilter(x => p(x) && q(x)) } // A helper for tails and inits. private def iterateUntilEmpty(f: Traversable[A
|
|
|
161 |
@uV] => Traversable[A @uV]): Iterator[Repr] = { val it = Iterator.iterate(thisCollection)(f) takeWhile (x => !x.isEmpty) it ++ Iterator(Nil)
|
|
|
162 |
map (newBuilder ++= _ result) } } </textarea>
|
|
|
163 |
</form>
|
|
|
164 |
<script>
|
|
|
165 |
var editor = CodeMirror.fromTextArea(document.getElementById("code"),
|
|
|
166 |
{
|
|
|
167 |
lineNumbers: true,
|
|
|
168 |
matchBrackets: true,
|
|
|
169 |
theme: "ambiance",
|
|
|
170 |
mode: "text/x-scala"
|
|
|
171 |
});
|
|
|
172 |
</script>
|
|
|
173 |
</article>
|