move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / clike / scala.html
1 <!doctype html>
2 <html>
3   <head>
4     <meta charset="utf-8">
5     <title>CodeMirror: C-like mode</title>
6     <link rel="stylesheet" href="../../lib/codemirror.css">
7     <link rel="stylesheet" href="../../theme/ambiance.css">
8     <script src="../../lib/codemirror.js"></script>
9     <script src="../../addon/edit/matchbrackets.js"></script>
10     <script src="clike.js"></script>
11     <link rel="stylesheet" href="../../doc/docs.css">
12     <style>
13       body
14       {
15         margin: 0;
16         padding: 0;
17         max-width:inherit;
18         height: 100%;
19       }
20       html, form, .CodeMirror, .CodeMirror-scroll
21       {
22         height: 100%;        
23       }
24     </style>
25   </head>
26   <body>
27 <form>
28 <textarea id="code" name="code">
29
30   /*                     __                                               *\
31   **     ________ ___   / /  ___     Scala API                            **
32   **    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
33   **  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
34   ** /____/\___/_/ |_/____/_/ | |                                         **
35   **                          |/                                          **
36   \*                                                                      */
37
38   package scala.collection
39
40   import generic._
41   import mutable.{ Builder, ListBuffer }
42   import annotation.{tailrec, migration, bridge}
43   import annotation.unchecked.{ uncheckedVariance => uV }
44   import parallel.ParIterable
45
46   /** A template trait for traversable collections of type `Traversable[A]`.
47    *  
48    *  $traversableInfo
49    *  @define mutability
50    *  @define traversableInfo
51    *  This is a base trait of all kinds of $mutability Scala collections. It
52    *  implements the behavior common to all collections, in terms of a method
53    *  `foreach` with signature:
54    * {{{
55    *     def foreach[U](f: Elem => U): Unit
56    * }}}
57    *  Collection classes mixing in this trait provide a concrete 
58    *  `foreach` method which traverses all the
59    *  elements contained in the collection, applying a given function to each.
60    *  They also need to provide a method `newBuilder`
61    *  which creates a builder for collections of the same kind.
62    *  
63    *  A traversable class might or might not have two properties: strictness
64    *  and orderedness. Neither is represented as a type.
65    *  
66    *  The instances of a strict collection class have all their elements
67    *  computed before they can be used as values. By contrast, instances of
68    *  a non-strict collection class may defer computation of some of their
69    *  elements until after the instance is available as a value.
70    *  A typical example of a non-strict collection class is a
71    *  <a href="../immutable/Stream.html" target="ContentFrame">
72    *  `scala.collection.immutable.Stream`</a>.
73    *  A more general class of examples are `TraversableViews`.
74    *  
75    *  If a collection is an instance of an ordered collection class, traversing
76    *  its elements with `foreach` will always visit elements in the
77    *  same order, even for different runs of the program. If the class is not
78    *  ordered, `foreach` can visit elements in different orders for
79    *  different runs (but it will keep the same order in the same run).'
80    * 
81    *  A typical example of a collection class which is not ordered is a
82    *  `HashMap` of objects. The traversal order for hash maps will
83    *  depend on the hash codes of its elements, and these hash codes might
84    *  differ from one run to the next. By contrast, a `LinkedHashMap`
85    *  is ordered because it's `foreach` method visits elements in the
86    *  order they were inserted into the `HashMap`.
87    *
88    *  @author Martin Odersky
89    *  @version 2.8
90    *  @since   2.8
91    *  @tparam A    the element type of the collection
92    *  @tparam Repr the type of the actual collection containing the elements.
93    *
94    *  @define Coll Traversable
95    *  @define coll traversable collection
96    */
97   trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr] 
98                                       with FilterMonadic[A, Repr]
99                                       with TraversableOnce[A]
100                                       with GenTraversableLike[A, Repr]
101                                       with Parallelizable[A, ParIterable[A]]
102   {
103     self =>
104
105     import Traversable.breaks._
106
107     /** The type implementing this traversable */
108     protected type Self = Repr
109
110     /** The collection of type $coll underlying this `TraversableLike` object.
111      *  By default this is implemented as the `TraversableLike` object itself,
112      *  but this can be overridden.
113      */
114     def repr: Repr = this.asInstanceOf[Repr]
115
116     /** The underlying collection seen as an instance of `$Coll`.
117      *  By default this is implemented as the current collection object itself,
118      *  but this can be overridden.
119      */
120     protected[this] def thisCollection: Traversable[A] = this.asInstanceOf[Traversable[A]]
121
122     /** A conversion from collections of type `Repr` to `$Coll` objects.
123      *  By default this is implemented as just a cast, but this can be overridden.
124      */
125     protected[this] def toCollection(repr: Repr): Traversable[A] = repr.asInstanceOf[Traversable[A]]
126
127     /** Creates a new builder for this collection type.
128      */
129     protected[this] def newBuilder: Builder[A, Repr]
130
131     protected[this] def parCombiner = ParIterable.newCombiner[A]
132
133     /** Applies a function `f` to all elements of this $coll.
134      *  
135      *    Note: this method underlies the implementation of most other bulk operations.
136      *    It's important to implement this method in an efficient way.
137      *  
138      *
139      *  @param  f   the function that is applied for its side-effect to every element.
140      *              The result of function `f` is discarded.
141      *              
142      *  @tparam  U  the type parameter describing the result of function `f`. 
143      *              This result will always be ignored. Typically `U` is `Unit`,
144      *              but this is not necessary.
145      *
146      *  @usecase def foreach(f: A => Unit): Unit
147      */
148     def foreach[U](f: A => U): Unit
149
150     /** Tests whether this $coll is empty.
151      *
152      *  @return    `true` if the $coll contain no elements, `false` otherwise.
153      */
154     def isEmpty: Boolean = {
155       var result = true
156       breakable {
157         for (x <- this) {
158           result = false
159           break
160         }
161       }
162       result
163     }
164
165     /** Tests whether this $coll is known to have a finite size.
166      *  All strict collections are known to have finite size. For a non-strict collection
167      *  such as `Stream`, the predicate returns `true` if all elements have been computed.
168      *  It returns `false` if the stream is not yet evaluated to the end.
169      *
170      *  Note: many collection methods will not work on collections of infinite sizes. 
171      *
172      *  @return  `true` if this collection is known to have finite size, `false` otherwise.
173      */
174     def hasDefiniteSize = true
175
176     def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
177       val b = bf(repr)
178       if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.seq.size)
179       b ++= thisCollection
180       b ++= that.seq
181       b.result
182     }
183
184     @bridge
185     def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
186       ++(that: GenTraversableOnce[B])(bf)
187
188     /** Concatenates this $coll with the elements of a traversable collection.
189      *  It differs from ++ in that the right operand determines the type of the
190      *  resulting collection rather than the left one.
191      * 
192      *  @param that   the traversable to append.
193      *  @tparam B     the element type of the returned collection. 
194      *  @tparam That  $thatinfo
195      *  @param bf     $bfinfo
196      *  @return       a new collection of type `That` which contains all elements
197      *                of this $coll followed by all elements of `that`.
198      * 
199      *  @usecase def ++:[B](that: TraversableOnce[B]): $Coll[B]
200      *  
201      *  @return       a new $coll which contains all elements of this $coll
202      *                followed by all elements of `that`.
203      */
204     def ++:[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
205       val b = bf(repr)
206       if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.size)
207       b ++= that
208       b ++= thisCollection
209       b.result
210     }
211
212     /** This overload exists because: for the implementation of ++: we should reuse
213      *  that of ++ because many collections override it with more efficient versions.
214      *  Since TraversableOnce has no '++' method, we have to implement that directly,
215      *  but Traversable and down can use the overload.
216      */
217     def ++:[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
218       (that ++ seq)(breakOut)
219
220     def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
221       val b = bf(repr)
222       b.sizeHint(this) 
223       for (x <- this) b += f(x)
224       b.result
225     }
226
227     def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
228       val b = bf(repr)
229       for (x <- this) b ++= f(x).seq
230       b.result
231     }
232
233     /** Selects all elements of this $coll which satisfy a predicate.
234      *
235      *  @param p     the predicate used to test elements.
236      *  @return      a new $coll consisting of all elements of this $coll that satisfy the given
237      *               predicate `p`. The order of the elements is preserved.
238      */
239     def filter(p: A => Boolean): Repr = {
240       val b = newBuilder
241       for (x <- this) 
242         if (p(x)) b += x
243       b.result
244     }
245
246     /** Selects all elements of this $coll which do not satisfy a predicate.
247      *
248      *  @param p     the predicate used to test elements.
249      *  @return      a new $coll consisting of all elements of this $coll that do not satisfy the given
250      *               predicate `p`. The order of the elements is preserved.
251      */
252     def filterNot(p: A => Boolean): Repr = filter(!p(_))
253
254     def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
255       val b = bf(repr)
256       for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
257       b.result
258     }
259
260     /** Builds a new collection by applying an option-valued function to all
261      *  elements of this $coll on which the function is defined.
262      *
263      *  @param f      the option-valued function which filters and maps the $coll.
264      *  @tparam B     the element type of the returned collection.
265      *  @tparam That  $thatinfo
266      *  @param bf     $bfinfo
267      *  @return       a new collection of type `That` resulting from applying the option-valued function
268      *                `f` to each element and collecting all defined results.
269      *                The order of the elements is preserved.
270      *
271      *  @usecase def filterMap[B](f: A => Option[B]): $Coll[B]
272      *  
273      *  @param pf     the partial function which filters and maps the $coll.
274      *  @return       a new $coll resulting from applying the given option-valued function
275      *                `f` to each element and collecting all defined results.
276      *                The order of the elements is preserved.
277     def filterMap[B, That](f: A => Option[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
278       val b = bf(repr)
279       for (x <- this) 
280         f(x) match {
281           case Some(y) => b += y
282           case _ =>
283         }
284       b.result
285     }
286      */
287
288     /** Partitions this $coll in two ${coll}s according to a predicate.
289      *
290      *  @param p the predicate on which to partition.
291      *  @return  a pair of ${coll}s: the first $coll consists of all elements that 
292      *           satisfy the predicate `p` and the second $coll consists of all elements
293      *           that don't. The relative order of the elements in the resulting ${coll}s
294      *           is the same as in the original $coll.
295      */
296     def partition(p: A => Boolean): (Repr, Repr) = {
297       val l, r = newBuilder
298       for (x <- this) (if (p(x)) l else r) += x
299       (l.result, r.result)
300     }
301
302     def groupBy[K](f: A => K): immutable.Map[K, Repr] = {
303       val m = mutable.Map.empty[K, Builder[A, Repr]]
304       for (elem <- this) {
305         val key = f(elem)
306         val bldr = m.getOrElseUpdate(key, newBuilder)
307         bldr += elem
308       }
309       val b = immutable.Map.newBuilder[K, Repr]
310       for ((k, v) <- m)
311         b += ((k, v.result))
312
313       b.result
314     }
315
316     /** Tests whether a predicate holds for all elements of this $coll.
317      *
318      *  $mayNotTerminateInf
319      *
320      *  @param   p     the predicate used to test elements.
321      *  @return        `true` if the given predicate `p` holds for all elements
322      *                 of this $coll, otherwise `false`.
323      */
324     def forall(p: A => Boolean): Boolean = {
325       var result = true
326       breakable {
327         for (x <- this)
328           if (!p(x)) { result = false; break }
329       }
330       result
331     }
332
333     /** Tests whether a predicate holds for some of the elements of this $coll.
334      *
335      *  $mayNotTerminateInf
336      *
337      *  @param   p     the predicate used to test elements.
338      *  @return        `true` if the given predicate `p` holds for some of the
339      *                 elements of this $coll, otherwise `false`.
340      */
341     def exists(p: A => Boolean): Boolean = {
342       var result = false
343       breakable {
344         for (x <- this)
345           if (p(x)) { result = true; break }
346       }
347       result
348     }
349
350     /** Finds the first element of the $coll satisfying a predicate, if any.
351      * 
352      *  $mayNotTerminateInf
353      *  $orderDependent
354      *
355      *  @param p    the predicate used to test elements.
356      *  @return     an option value containing the first element in the $coll
357      *              that satisfies `p`, or `None` if none exists.
358      */
359     def find(p: A => Boolean): Option[A] = {
360       var result: Option[A] = None
361       breakable {
362         for (x <- this)
363           if (p(x)) { result = Some(x); break }
364       }
365       result
366     }
367
368     def scan[B >: A, That](z: B)(op: (B, B) => B)(implicit cbf: CanBuildFrom[Repr, B, That]): That = scanLeft(z)(op)
369
370     def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
371       val b = bf(repr)
372       b.sizeHint(this, 1)
373       var acc = z
374       b += acc
375       for (x <- this) { acc = op(acc, x); b += acc }
376       b.result
377     }
378
379     @migration(2, 9,
380       "This scanRight definition has changed in 2.9.\n" +
381       "The previous behavior can be reproduced with scanRight.reverse."
382     )
383     def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
384       var scanned = List(z)
385       var acc = z
386       for (x <- reversed) {
387         acc = op(x, acc)
388         scanned ::= acc
389       }
390       val b = bf(repr)
391       for (elem <- scanned) b += elem
392       b.result
393     }
394
395     /** Selects the first element of this $coll.
396      *  $orderDependent
397      *  @return  the first element of this $coll.
398      *  @throws `NoSuchElementException` if the $coll is empty.
399      */
400     def head: A = {
401       var result: () => A = () => throw new NoSuchElementException
402       breakable {
403         for (x <- this) {
404           result = () => x
405           break
406         }
407       }
408       result()
409     }
410
411     /** Optionally selects the first element.
412      *  $orderDependent
413      *  @return  the first element of this $coll if it is nonempty, `None` if it is empty.
414      */
415     def headOption: Option[A] = if (isEmpty) None else Some(head)
416
417     /** Selects all elements except the first.
418      *  $orderDependent
419      *  @return  a $coll consisting of all elements of this $coll
420      *           except the first one.
421      *  @throws `UnsupportedOperationException` if the $coll is empty.
422      */ 
423     override def tail: Repr = {
424       if (isEmpty) throw new UnsupportedOperationException("empty.tail")
425       drop(1)
426     }
427
428     /** Selects the last element.
429       * $orderDependent
430       * @return The last element of this $coll.
431       * @throws NoSuchElementException If the $coll is empty.
432       */
433     def last: A = {
434       var lst = head
435       for (x <- this)
436         lst = x
437       lst
438     }
439
440     /** Optionally selects the last element.
441      *  $orderDependent
442      *  @return  the last element of this $coll$ if it is nonempty, `None` if it is empty.
443      */
444     def lastOption: Option[A] = if (isEmpty) None else Some(last)
445
446     /** Selects all elements except the last.
447      *  $orderDependent
448      *  @return  a $coll consisting of all elements of this $coll
449      *           except the last one.
450      *  @throws `UnsupportedOperationException` if the $coll is empty.
451      */
452     def init: Repr = {
453       if (isEmpty) throw new UnsupportedOperationException("empty.init")
454       var lst = head
455       var follow = false
456       val b = newBuilder
457       b.sizeHint(this, -1)
458       for (x <- this.seq) {
459         if (follow) b += lst
460         else follow = true
461         lst = x
462       }
463       b.result
464     }
465
466     def take(n: Int): Repr = slice(0, n)
467
468     def drop(n: Int): Repr = 
469       if (n <= 0) {
470         val b = newBuilder
471         b.sizeHint(this)
472         b ++= thisCollection result
473       }
474       else sliceWithKnownDelta(n, Int.MaxValue, -n)
475
476     def slice(from: Int, until: Int): Repr = sliceWithKnownBound(math.max(from, 0), until)
477
478     // Precondition: from >= 0, until > 0, builder already configured for building.
479     private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = {
480       var i = 0
481       breakable {
482         for (x <- this.seq) {
483           if (i >= from) b += x
484           i += 1
485           if (i >= until) break
486         }
487       }
488       b.result
489     }
490     // Precondition: from >= 0
491     private[scala] def sliceWithKnownDelta(from: Int, until: Int, delta: Int): Repr = {
492       val b = newBuilder
493       if (until <= from) b.result
494       else {
495         b.sizeHint(this, delta)
496         sliceInternal(from, until, b)
497       }
498     }
499     // Precondition: from >= 0
500     private[scala] def sliceWithKnownBound(from: Int, until: Int): Repr = {
501       val b = newBuilder
502       if (until <= from) b.result
503       else {
504         b.sizeHintBounded(until - from, this)      
505         sliceInternal(from, until, b)
506       }
507     }
508
509     def takeWhile(p: A => Boolean): Repr = {
510       val b = newBuilder
511       breakable {
512         for (x <- this) {
513           if (!p(x)) break
514           b += x
515         }
516       }
517       b.result
518     }
519
520     def dropWhile(p: A => Boolean): Repr = {
521       val b = newBuilder
522       var go = false
523       for (x <- this) {
524         if (!p(x)) go = true
525         if (go) b += x
526       }
527       b.result
528     }
529
530     def span(p: A => Boolean): (Repr, Repr) = {
531       val l, r = newBuilder
532       var toLeft = true
533       for (x <- this) {
534         toLeft = toLeft && p(x)
535         (if (toLeft) l else r) += x
536       }
537       (l.result, r.result)
538     }
539
540     def splitAt(n: Int): (Repr, Repr) = {
541       val l, r = newBuilder
542       l.sizeHintBounded(n, this)
543       if (n >= 0) r.sizeHint(this, -n)
544       var i = 0
545       for (x <- this) {
546         (if (i < n) l else r) += x
547         i += 1
548       }
549       (l.result, r.result)
550     }
551
552     /** Iterates over the tails of this $coll. The first value will be this
553      *  $coll and the final one will be an empty $coll, with the intervening
554      *  values the results of successive applications of `tail`.
555      *
556      *  @return   an iterator over all the tails of this $coll
557      *  @example  `List(1,2,3).tails = Iterator(List(1,2,3), List(2,3), List(3), Nil)`
558      */  
559     def tails: Iterator[Repr] = iterateUntilEmpty(_.tail)
560
561     /** Iterates over the inits of this $coll. The first value will be this
562      *  $coll and the final one will be an empty $coll, with the intervening
563      *  values the results of successive applications of `init`.
564      *
565      *  @return  an iterator over all the inits of this $coll
566      *  @example  `List(1,2,3).inits = Iterator(List(1,2,3), List(1,2), List(1), Nil)`
567      */
568     def inits: Iterator[Repr] = iterateUntilEmpty(_.init)
569
570     /** Copies elements of this $coll to an array.
571      *  Fills the given array `xs` with at most `len` elements of
572      *  this $coll, starting at position `start`.
573      *  Copying will stop once either the end of the current $coll is reached,
574      *  or the end of the array is reached, or `len` elements have been copied.
575      *
576      *  $willNotTerminateInf
577      * 
578      *  @param  xs     the array to fill.
579      *  @param  start  the starting index.
580      *  @param  len    the maximal number of elements to copy.
581      *  @tparam B      the type of the elements of the array. 
582      * 
583      *
584      *  @usecase def copyToArray(xs: Array[A], start: Int, len: Int): Unit
585      */
586     def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) {
587       var i = start
588       val end = (start + len) min xs.length
589       breakable {
590         for (x <- this) {
591           if (i >= end) break
592           xs(i) = x
593           i += 1
594         }
595       }
596     }
597
598     def toTraversable: Traversable[A] = thisCollection
599     def toIterator: Iterator[A] = toStream.iterator
600     def toStream: Stream[A] = toBuffer.toStream
601
602     /** Converts this $coll to a string.
603      *
604      *  @return   a string representation of this collection. By default this
605      *            string consists of the `stringPrefix` of this $coll,
606      *            followed by all elements separated by commas and enclosed in parentheses.
607      */
608     override def toString = mkString(stringPrefix + "(", ", ", ")")
609
610     /** Defines the prefix of this object's `toString` representation.
611      *
612      *  @return  a string representation which starts the result of `toString`
613      *           applied to this $coll. By default the string prefix is the
614      *           simple name of the collection class $coll.
615      */
616     def stringPrefix : String = {
617       var string = repr.asInstanceOf[AnyRef].getClass.getName
618       val idx1 = string.lastIndexOf('.' : Int)
619       if (idx1 != -1) string = string.substring(idx1 + 1)
620       val idx2 = string.indexOf('$')
621       if (idx2 != -1) string = string.substring(0, idx2)
622       string
623     }
624
625     /** Creates a non-strict view of this $coll.
626      * 
627      *  @return a non-strict view of this $coll.
628      */
629     def view = new TraversableView[A, Repr] {
630       protected lazy val underlying = self.repr
631       override def foreach[U](f: A => U) = self foreach f
632     }
633
634     /** Creates a non-strict view of a slice of this $coll.
635      *
636      *  Note: the difference between `view` and `slice` is that `view` produces
637      *        a view of the current $coll, whereas `slice` produces a new $coll.
638      * 
639      *  Note: `view(from, to)` is equivalent to `view.slice(from, to)`
640      *  $orderDependent
641      * 
642      *  @param from   the index of the first element of the view
643      *  @param until  the index of the element following the view
644      *  @return a non-strict view of a slice of this $coll, starting at index `from`
645      *  and extending up to (but not including) index `until`.
646      */
647     def view(from: Int, until: Int): TraversableView[A, Repr] = view.slice(from, until)
648
649     /** Creates a non-strict filter of this $coll.
650      *
651      *  Note: the difference between `c filter p` and `c withFilter p` is that
652      *        the former creates a new collection, whereas the latter only
653      *        restricts the domain of subsequent `map`, `flatMap`, `foreach`,
654      *        and `withFilter` operations.
655      *  $orderDependent
656      * 
657      *  @param p   the predicate used to test elements.
658      *  @return    an object of class `WithFilter`, which supports
659      *             `map`, `flatMap`, `foreach`, and `withFilter` operations.
660      *             All these operations apply to those elements of this $coll which
661      *             satisfy the predicate `p`.
662      */
663     def withFilter(p: A => Boolean): FilterMonadic[A, Repr] = new WithFilter(p)
664
665     /** A class supporting filtered operations. Instances of this class are
666      *  returned by method `withFilter`.
667      */
668     class WithFilter(p: A => Boolean) extends FilterMonadic[A, Repr] {
669
670       /** Builds a new collection by applying a function to all elements of the
671        *  outer $coll containing this `WithFilter` instance that satisfy predicate `p`.
672        *
673        *  @param f      the function to apply to each element.
674        *  @tparam B     the element type of the returned collection.
675        *  @tparam That  $thatinfo
676        *  @param bf     $bfinfo
677        *  @return       a new collection of type `That` resulting from applying
678        *                the given function `f` to each element of the outer $coll
679        *                that satisfies predicate `p` and collecting the results.
680        *
681        *  @usecase def map[B](f: A => B): $Coll[B] 
682        *  
683        *  @return       a new $coll resulting from applying the given function
684        *                `f` to each element of the outer $coll that satisfies
685        *                predicate `p` and collecting the results.
686        */
687       def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
688         val b = bf(repr)
689         for (x <- self) 
690           if (p(x)) b += f(x)
691         b.result
692       }
693
694       /** Builds a new collection by applying a function to all elements of the
695        *  outer $coll containing this `WithFilter` instance that satisfy
696        *  predicate `p` and concatenating the results. 
697        *
698        *  @param f      the function to apply to each element.
699        *  @tparam B     the element type of the returned collection.
700        *  @tparam That  $thatinfo
701        *  @param bf     $bfinfo
702        *  @return       a new collection of type `That` resulting from applying
703        *                the given collection-valued function `f` to each element
704        *                of the outer $coll that satisfies predicate `p` and
705        *                concatenating the results.
706        *
707        *  @usecase def flatMap[B](f: A => TraversableOnce[B]): $Coll[B]
708        * 
709        *  @return       a new $coll resulting from applying the given collection-valued function
710        *                `f` to each element of the outer $coll that satisfies predicate `p` and concatenating the results.
711        */
712       def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
713         val b = bf(repr)
714         for (x <- self) 
715           if (p(x)) b ++= f(x).seq
716         b.result
717       }
718
719       /** Applies a function `f` to all elements of the outer $coll containing
720        *  this `WithFilter` instance that satisfy predicate `p`.
721        *
722        *  @param  f   the function that is applied for its side-effect to every element.
723        *              The result of function `f` is discarded.
724        *              
725        *  @tparam  U  the type parameter describing the result of function `f`. 
726        *              This result will always be ignored. Typically `U` is `Unit`,
727        *              but this is not necessary.
728        *
729        *  @usecase def foreach(f: A => Unit): Unit
730        */   
731       def foreach[U](f: A => U): Unit = 
732         for (x <- self) 
733           if (p(x)) f(x)
734
735       /** Further refines the filter for this $coll.
736        *
737        *  @param q   the predicate used to test elements.
738        *  @return    an object of class `WithFilter`, which supports
739        *             `map`, `flatMap`, `foreach`, and `withFilter` operations.
740        *             All these operations apply to those elements of this $coll which
741        *             satisfy the predicate `q` in addition to the predicate `p`.
742        */
743       def withFilter(q: A => Boolean): WithFilter = 
744         new WithFilter(x => p(x) && q(x))
745     }
746
747     // A helper for tails and inits.
748     private def iterateUntilEmpty(f: Traversable[A @uV] => Traversable[A @uV]): Iterator[Repr] = {
749       val it = Iterator.iterate(thisCollection)(f) takeWhile (x => !x.isEmpty)
750       it ++ Iterator(Nil) map (newBuilder ++= _ result)
751     }
752   }
753
754
755 </textarea>
756 </form>
757
758     <script>
759       var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
760         lineNumbers: true,
761         matchBrackets: true,
762         theme: "ambiance",
763         mode: "text/x-scala"
764       });
765     </script>
766   </body>
767 </html>