Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wp-file-.../lib/codemirr.../mode/clike
File: scala.html
<!doctype html>
[0] Fix | Delete
[1] Fix | Delete
<title>CodeMirror: Scala mode</title>
[2] Fix | Delete
<meta charset="utf-8"/>
[3] Fix | Delete
<link rel=stylesheet href="../../doc/docs.css">
[4] Fix | Delete
[5] Fix | Delete
<link rel="stylesheet" href="../../lib/codemirror.css">
[6] Fix | Delete
<link rel="stylesheet" href="../../theme/ambiance.css">
[7] Fix | Delete
<script src="../../lib/codemirror.js"></script>
[8] Fix | Delete
<script src="../../addon/edit/matchbrackets.js"></script>
[9] Fix | Delete
<script src="clike.js"></script>
[10] Fix | Delete
<div id=nav>
[11] Fix | Delete
<a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
[12] Fix | Delete
[13] Fix | Delete
<ul>
[14] Fix | Delete
<li><a href="../../index.html">Home</a>
[15] Fix | Delete
<li><a href="../../doc/manual.html">Manual</a>
[16] Fix | Delete
<li><a href="https://github.com/codemirror/codemirror">Code</a>
[17] Fix | Delete
</ul>
[18] Fix | Delete
<ul>
[19] Fix | Delete
<li><a href="../index.html">Language modes</a>
[20] Fix | Delete
<li><a class=active href="#">Scala</a>
[21] Fix | Delete
</ul>
[22] Fix | Delete
</div>
[23] Fix | Delete
[24] Fix | Delete
<article>
[25] Fix | Delete
<h2>Scala mode</h2>
[26] Fix | Delete
<form>
[27] Fix | Delete
<textarea id="code" name="code">
[28] Fix | Delete
[29] Fix | Delete
/* __ *\
[30] Fix | Delete
** ________ ___ / / ___ Scala API **
[31] Fix | Delete
** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
[32] Fix | Delete
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
[33] Fix | Delete
** /____/\___/_/ |_/____/_/ | | **
[34] Fix | Delete
** |/ **
[35] Fix | Delete
\* */
[36] Fix | Delete
[37] Fix | Delete
package scala.collection
[38] Fix | Delete
[39] Fix | Delete
import generic._
[40] Fix | Delete
import mutable.{ Builder, ListBuffer }
[41] Fix | Delete
import annotation.{tailrec, migration, bridge}
[42] Fix | Delete
import annotation.unchecked.{ uncheckedVariance => uV }
[43] Fix | Delete
import parallel.ParIterable
[44] Fix | Delete
[45] Fix | Delete
/** A template trait for traversable collections of type `Traversable[A]`.
[46] Fix | Delete
*
[47] Fix | Delete
* $traversableInfo
[48] Fix | Delete
* @define mutability
[49] Fix | Delete
* @define traversableInfo
[50] Fix | Delete
* This is a base trait of all kinds of $mutability Scala collections. It
[51] Fix | Delete
* implements the behavior common to all collections, in terms of a method
[52] Fix | Delete
* `foreach` with signature:
[53] Fix | Delete
* {{{
[54] Fix | Delete
* def foreach[U](f: Elem => U): Unit
[55] Fix | Delete
* }}}
[56] Fix | Delete
* Collection classes mixing in this trait provide a concrete
[57] Fix | Delete
* `foreach` method which traverses all the
[58] Fix | Delete
* elements contained in the collection, applying a given function to each.
[59] Fix | Delete
* They also need to provide a method `newBuilder`
[60] Fix | Delete
* which creates a builder for collections of the same kind.
[61] Fix | Delete
*
[62] Fix | Delete
* A traversable class might or might not have two properties: strictness
[63] Fix | Delete
* and orderedness. Neither is represented as a type.
[64] Fix | Delete
*
[65] Fix | Delete
* The instances of a strict collection class have all their elements
[66] Fix | Delete
* computed before they can be used as values. By contrast, instances of
[67] Fix | Delete
* a non-strict collection class may defer computation of some of their
[68] Fix | Delete
* elements until after the instance is available as a value.
[69] Fix | Delete
* A typical example of a non-strict collection class is a
[70] Fix | Delete
* <a href="../immutable/Stream.html" target="ContentFrame">
[71] Fix | Delete
* `scala.collection.immutable.Stream`</a>.
[72] Fix | Delete
* A more general class of examples are `TraversableViews`.
[73] Fix | Delete
*
[74] Fix | Delete
* If a collection is an instance of an ordered collection class, traversing
[75] Fix | Delete
* its elements with `foreach` will always visit elements in the
[76] Fix | Delete
* same order, even for different runs of the program. If the class is not
[77] Fix | Delete
* ordered, `foreach` can visit elements in different orders for
[78] Fix | Delete
* different runs (but it will keep the same order in the same run).'
[79] Fix | Delete
*
[80] Fix | Delete
* A typical example of a collection class which is not ordered is a
[81] Fix | Delete
* `HashMap` of objects. The traversal order for hash maps will
[82] Fix | Delete
* depend on the hash codes of its elements, and these hash codes might
[83] Fix | Delete
* differ from one run to the next. By contrast, a `LinkedHashMap`
[84] Fix | Delete
* is ordered because it's `foreach` method visits elements in the
[85] Fix | Delete
* order they were inserted into the `HashMap`.
[86] Fix | Delete
*
[87] Fix | Delete
* @author Martin Odersky
[88] Fix | Delete
* @version 2.8
[89] Fix | Delete
* @since 2.8
[90] Fix | Delete
* @tparam A the element type of the collection
[91] Fix | Delete
* @tparam Repr the type of the actual collection containing the elements.
[92] Fix | Delete
*
[93] Fix | Delete
* @define Coll Traversable
[94] Fix | Delete
* @define coll traversable collection
[95] Fix | Delete
*/
[96] Fix | Delete
trait TraversableLike[+A, +Repr] extends HasNewBuilder[A, Repr]
[97] Fix | Delete
with FilterMonadic[A, Repr]
[98] Fix | Delete
with TraversableOnce[A]
[99] Fix | Delete
with GenTraversableLike[A, Repr]
[100] Fix | Delete
with Parallelizable[A, ParIterable[A]]
[101] Fix | Delete
{
[102] Fix | Delete
self =>
[103] Fix | Delete
[104] Fix | Delete
import Traversable.breaks._
[105] Fix | Delete
[106] Fix | Delete
/** The type implementing this traversable */
[107] Fix | Delete
protected type Self = Repr
[108] Fix | Delete
[109] Fix | Delete
/** The collection of type $coll underlying this `TraversableLike` object.
[110] Fix | Delete
* By default this is implemented as the `TraversableLike` object itself,
[111] Fix | Delete
* but this can be overridden.
[112] Fix | Delete
*/
[113] Fix | Delete
def repr: Repr = this.asInstanceOf[Repr]
[114] Fix | Delete
[115] Fix | Delete
/** The underlying collection seen as an instance of `$Coll`.
[116] Fix | Delete
* By default this is implemented as the current collection object itself,
[117] Fix | Delete
* but this can be overridden.
[118] Fix | Delete
*/
[119] Fix | Delete
protected[this] def thisCollection: Traversable[A] = this.asInstanceOf[Traversable[A]]
[120] Fix | Delete
[121] Fix | Delete
/** A conversion from collections of type `Repr` to `$Coll` objects.
[122] Fix | Delete
* By default this is implemented as just a cast, but this can be overridden.
[123] Fix | Delete
*/
[124] Fix | Delete
protected[this] def toCollection(repr: Repr): Traversable[A] = repr.asInstanceOf[Traversable[A]]
[125] Fix | Delete
[126] Fix | Delete
/** Creates a new builder for this collection type.
[127] Fix | Delete
*/
[128] Fix | Delete
protected[this] def newBuilder: Builder[A, Repr]
[129] Fix | Delete
[130] Fix | Delete
protected[this] def parCombiner = ParIterable.newCombiner[A]
[131] Fix | Delete
[132] Fix | Delete
/** Applies a function `f` to all elements of this $coll.
[133] Fix | Delete
*
[134] Fix | Delete
* Note: this method underlies the implementation of most other bulk operations.
[135] Fix | Delete
* It's important to implement this method in an efficient way.
[136] Fix | Delete
*
[137] Fix | Delete
*
[138] Fix | Delete
* @param f the function that is applied for its side-effect to every element.
[139] Fix | Delete
* The result of function `f` is discarded.
[140] Fix | Delete
*
[141] Fix | Delete
* @tparam U the type parameter describing the result of function `f`.
[142] Fix | Delete
* This result will always be ignored. Typically `U` is `Unit`,
[143] Fix | Delete
* but this is not necessary.
[144] Fix | Delete
*
[145] Fix | Delete
* @usecase def foreach(f: A => Unit): Unit
[146] Fix | Delete
*/
[147] Fix | Delete
def foreach[U](f: A => U): Unit
[148] Fix | Delete
[149] Fix | Delete
/** Tests whether this $coll is empty.
[150] Fix | Delete
*
[151] Fix | Delete
* @return `true` if the $coll contain no elements, `false` otherwise.
[152] Fix | Delete
*/
[153] Fix | Delete
def isEmpty: Boolean = {
[154] Fix | Delete
var result = true
[155] Fix | Delete
breakable {
[156] Fix | Delete
for (x <- this) {
[157] Fix | Delete
result = false
[158] Fix | Delete
break
[159] Fix | Delete
}
[160] Fix | Delete
}
[161] Fix | Delete
result
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
/** Tests whether this $coll is known to have a finite size.
[165] Fix | Delete
* All strict collections are known to have finite size. For a non-strict collection
[166] Fix | Delete
* such as `Stream`, the predicate returns `true` if all elements have been computed.
[167] Fix | Delete
* It returns `false` if the stream is not yet evaluated to the end.
[168] Fix | Delete
*
[169] Fix | Delete
* Note: many collection methods will not work on collections of infinite sizes.
[170] Fix | Delete
*
[171] Fix | Delete
* @return `true` if this collection is known to have finite size, `false` otherwise.
[172] Fix | Delete
*/
[173] Fix | Delete
def hasDefiniteSize = true
[174] Fix | Delete
[175] Fix | Delete
def ++[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[176] Fix | Delete
val b = bf(repr)
[177] Fix | Delete
if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.seq.size)
[178] Fix | Delete
b ++= thisCollection
[179] Fix | Delete
b ++= that.seq
[180] Fix | Delete
b.result
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
@bridge
[184] Fix | Delete
def ++[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
[185] Fix | Delete
++(that: GenTraversableOnce[B])(bf)
[186] Fix | Delete
[187] Fix | Delete
/** Concatenates this $coll with the elements of a traversable collection.
[188] Fix | Delete
* It differs from ++ in that the right operand determines the type of the
[189] Fix | Delete
* resulting collection rather than the left one.
[190] Fix | Delete
*
[191] Fix | Delete
* @param that the traversable to append.
[192] Fix | Delete
* @tparam B the element type of the returned collection.
[193] Fix | Delete
* @tparam That $thatinfo
[194] Fix | Delete
* @param bf $bfinfo
[195] Fix | Delete
* @return a new collection of type `That` which contains all elements
[196] Fix | Delete
* of this $coll followed by all elements of `that`.
[197] Fix | Delete
*
[198] Fix | Delete
* @usecase def ++:[B](that: TraversableOnce[B]): $Coll[B]
[199] Fix | Delete
*
[200] Fix | Delete
* @return a new $coll which contains all elements of this $coll
[201] Fix | Delete
* followed by all elements of `that`.
[202] Fix | Delete
*/
[203] Fix | Delete
def ++:[B >: A, That](that: TraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[204] Fix | Delete
val b = bf(repr)
[205] Fix | Delete
if (that.isInstanceOf[IndexedSeqLike[_, _]]) b.sizeHint(this, that.size)
[206] Fix | Delete
b ++= that
[207] Fix | Delete
b ++= thisCollection
[208] Fix | Delete
b.result
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
/** This overload exists because: for the implementation of ++: we should reuse
[212] Fix | Delete
* that of ++ because many collections override it with more efficient versions.
[213] Fix | Delete
* Since TraversableOnce has no '++' method, we have to implement that directly,
[214] Fix | Delete
* but Traversable and down can use the overload.
[215] Fix | Delete
*/
[216] Fix | Delete
def ++:[B >: A, That](that: Traversable[B])(implicit bf: CanBuildFrom[Repr, B, That]): That =
[217] Fix | Delete
(that ++ seq)(breakOut)
[218] Fix | Delete
[219] Fix | Delete
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[220] Fix | Delete
val b = bf(repr)
[221] Fix | Delete
b.sizeHint(this)
[222] Fix | Delete
for (x <- this) b += f(x)
[223] Fix | Delete
b.result
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[227] Fix | Delete
val b = bf(repr)
[228] Fix | Delete
for (x <- this) b ++= f(x).seq
[229] Fix | Delete
b.result
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
/** Selects all elements of this $coll which satisfy a predicate.
[233] Fix | Delete
*
[234] Fix | Delete
* @param p the predicate used to test elements.
[235] Fix | Delete
* @return a new $coll consisting of all elements of this $coll that satisfy the given
[236] Fix | Delete
* predicate `p`. The order of the elements is preserved.
[237] Fix | Delete
*/
[238] Fix | Delete
def filter(p: A => Boolean): Repr = {
[239] Fix | Delete
val b = newBuilder
[240] Fix | Delete
for (x <- this)
[241] Fix | Delete
if (p(x)) b += x
[242] Fix | Delete
b.result
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/** Selects all elements of this $coll which do not satisfy a predicate.
[246] Fix | Delete
*
[247] Fix | Delete
* @param p the predicate used to test elements.
[248] Fix | Delete
* @return a new $coll consisting of all elements of this $coll that do not satisfy the given
[249] Fix | Delete
* predicate `p`. The order of the elements is preserved.
[250] Fix | Delete
*/
[251] Fix | Delete
def filterNot(p: A => Boolean): Repr = filter(!p(_))
[252] Fix | Delete
[253] Fix | Delete
def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[254] Fix | Delete
val b = bf(repr)
[255] Fix | Delete
for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
[256] Fix | Delete
b.result
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
/** Builds a new collection by applying an option-valued function to all
[260] Fix | Delete
* elements of this $coll on which the function is defined.
[261] Fix | Delete
*
[262] Fix | Delete
* @param f the option-valued function which filters and maps the $coll.
[263] Fix | Delete
* @tparam B the element type of the returned collection.
[264] Fix | Delete
* @tparam That $thatinfo
[265] Fix | Delete
* @param bf $bfinfo
[266] Fix | Delete
* @return a new collection of type `That` resulting from applying the option-valued function
[267] Fix | Delete
* `f` to each element and collecting all defined results.
[268] Fix | Delete
* The order of the elements is preserved.
[269] Fix | Delete
*
[270] Fix | Delete
* @usecase def filterMap[B](f: A => Option[B]): $Coll[B]
[271] Fix | Delete
*
[272] Fix | Delete
* @param pf the partial function which filters and maps the $coll.
[273] Fix | Delete
* @return a new $coll resulting from applying the given option-valued function
[274] Fix | Delete
* `f` to each element and collecting all defined results.
[275] Fix | Delete
* The order of the elements is preserved.
[276] Fix | Delete
def filterMap[B, That](f: A => Option[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[277] Fix | Delete
val b = bf(repr)
[278] Fix | Delete
for (x <- this)
[279] Fix | Delete
f(x) match {
[280] Fix | Delete
case Some(y) => b += y
[281] Fix | Delete
case _ =>
[282] Fix | Delete
}
[283] Fix | Delete
b.result
[284] Fix | Delete
}
[285] Fix | Delete
*/
[286] Fix | Delete
[287] Fix | Delete
/** Partitions this $coll in two ${coll}s according to a predicate.
[288] Fix | Delete
*
[289] Fix | Delete
* @param p the predicate on which to partition.
[290] Fix | Delete
* @return a pair of ${coll}s: the first $coll consists of all elements that
[291] Fix | Delete
* satisfy the predicate `p` and the second $coll consists of all elements
[292] Fix | Delete
* that don't. The relative order of the elements in the resulting ${coll}s
[293] Fix | Delete
* is the same as in the original $coll.
[294] Fix | Delete
*/
[295] Fix | Delete
def partition(p: A => Boolean): (Repr, Repr) = {
[296] Fix | Delete
val l, r = newBuilder
[297] Fix | Delete
for (x <- this) (if (p(x)) l else r) += x
[298] Fix | Delete
(l.result, r.result)
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
def groupBy[K](f: A => K): immutable.Map[K, Repr] = {
[302] Fix | Delete
val m = mutable.Map.empty[K, Builder[A, Repr]]
[303] Fix | Delete
for (elem <- this) {
[304] Fix | Delete
val key = f(elem)
[305] Fix | Delete
val bldr = m.getOrElseUpdate(key, newBuilder)
[306] Fix | Delete
bldr += elem
[307] Fix | Delete
}
[308] Fix | Delete
val b = immutable.Map.newBuilder[K, Repr]
[309] Fix | Delete
for ((k, v) <- m)
[310] Fix | Delete
b += ((k, v.result))
[311] Fix | Delete
[312] Fix | Delete
b.result
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
/** Tests whether a predicate holds for all elements of this $coll.
[316] Fix | Delete
*
[317] Fix | Delete
* $mayNotTerminateInf
[318] Fix | Delete
*
[319] Fix | Delete
* @param p the predicate used to test elements.
[320] Fix | Delete
* @return `true` if the given predicate `p` holds for all elements
[321] Fix | Delete
* of this $coll, otherwise `false`.
[322] Fix | Delete
*/
[323] Fix | Delete
def forall(p: A => Boolean): Boolean = {
[324] Fix | Delete
var result = true
[325] Fix | Delete
breakable {
[326] Fix | Delete
for (x <- this)
[327] Fix | Delete
if (!p(x)) { result = false; break }
[328] Fix | Delete
}
[329] Fix | Delete
result
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/** Tests whether a predicate holds for some of the elements of this $coll.
[333] Fix | Delete
*
[334] Fix | Delete
* $mayNotTerminateInf
[335] Fix | Delete
*
[336] Fix | Delete
* @param p the predicate used to test elements.
[337] Fix | Delete
* @return `true` if the given predicate `p` holds for some of the
[338] Fix | Delete
* elements of this $coll, otherwise `false`.
[339] Fix | Delete
*/
[340] Fix | Delete
def exists(p: A => Boolean): Boolean = {
[341] Fix | Delete
var result = false
[342] Fix | Delete
breakable {
[343] Fix | Delete
for (x <- this)
[344] Fix | Delete
if (p(x)) { result = true; break }
[345] Fix | Delete
}
[346] Fix | Delete
result
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
/** Finds the first element of the $coll satisfying a predicate, if any.
[350] Fix | Delete
*
[351] Fix | Delete
* $mayNotTerminateInf
[352] Fix | Delete
* $orderDependent
[353] Fix | Delete
*
[354] Fix | Delete
* @param p the predicate used to test elements.
[355] Fix | Delete
* @return an option value containing the first element in the $coll
[356] Fix | Delete
* that satisfies `p`, or `None` if none exists.
[357] Fix | Delete
*/
[358] Fix | Delete
def find(p: A => Boolean): Option[A] = {
[359] Fix | Delete
var result: Option[A] = None
[360] Fix | Delete
breakable {
[361] Fix | Delete
for (x <- this)
[362] Fix | Delete
if (p(x)) { result = Some(x); break }
[363] Fix | Delete
}
[364] Fix | Delete
result
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
def scan[B >: A, That](z: B)(op: (B, B) => B)(implicit cbf: CanBuildFrom[Repr, B, That]): That = scanLeft(z)(op)
[368] Fix | Delete
[369] Fix | Delete
def scanLeft[B, That](z: B)(op: (B, A) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[370] Fix | Delete
val b = bf(repr)
[371] Fix | Delete
b.sizeHint(this, 1)
[372] Fix | Delete
var acc = z
[373] Fix | Delete
b += acc
[374] Fix | Delete
for (x <- this) { acc = op(acc, x); b += acc }
[375] Fix | Delete
b.result
[376] Fix | Delete
}
[377] Fix | Delete
[378] Fix | Delete
@migration(2, 9,
[379] Fix | Delete
"This scanRight definition has changed in 2.9.\n" +
[380] Fix | Delete
"The previous behavior can be reproduced with scanRight.reverse."
[381] Fix | Delete
)
[382] Fix | Delete
def scanRight[B, That](z: B)(op: (A, B) => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
[383] Fix | Delete
var scanned = List(z)
[384] Fix | Delete
var acc = z
[385] Fix | Delete
for (x <- reversed) {
[386] Fix | Delete
acc = op(x, acc)
[387] Fix | Delete
scanned ::= acc
[388] Fix | Delete
}
[389] Fix | Delete
val b = bf(repr)
[390] Fix | Delete
for (elem <- scanned) b += elem
[391] Fix | Delete
b.result
[392] Fix | Delete
}
[393] Fix | Delete
[394] Fix | Delete
/** Selects the first element of this $coll.
[395] Fix | Delete
* $orderDependent
[396] Fix | Delete
* @return the first element of this $coll.
[397] Fix | Delete
* @throws `NoSuchElementException` if the $coll is empty.
[398] Fix | Delete
*/
[399] Fix | Delete
def head: A = {
[400] Fix | Delete
var result: () => A = () => throw new NoSuchElementException
[401] Fix | Delete
breakable {
[402] Fix | Delete
for (x <- this) {
[403] Fix | Delete
result = () => x
[404] Fix | Delete
break
[405] Fix | Delete
}
[406] Fix | Delete
}
[407] Fix | Delete
result()
[408] Fix | Delete
}
[409] Fix | Delete
[410] Fix | Delete
/** Optionally selects the first element.
[411] Fix | Delete
* $orderDependent
[412] Fix | Delete
* @return the first element of this $coll if it is nonempty, `None` if it is empty.
[413] Fix | Delete
*/
[414] Fix | Delete
def headOption: Option[A] = if (isEmpty) None else Some(head)
[415] Fix | Delete
[416] Fix | Delete
/** Selects all elements except the first.
[417] Fix | Delete
* $orderDependent
[418] Fix | Delete
* @return a $coll consisting of all elements of this $coll
[419] Fix | Delete
* except the first one.
[420] Fix | Delete
* @throws `UnsupportedOperationException` if the $coll is empty.
[421] Fix | Delete
*/
[422] Fix | Delete
override def tail: Repr = {
[423] Fix | Delete
if (isEmpty) throw new UnsupportedOperationException("empty.tail")
[424] Fix | Delete
drop(1)
[425] Fix | Delete
}
[426] Fix | Delete
[427] Fix | Delete
/** Selects the last element.
[428] Fix | Delete
* $orderDependent
[429] Fix | Delete
* @return The last element of this $coll.
[430] Fix | Delete
* @throws NoSuchElementException If the $coll is empty.
[431] Fix | Delete
*/
[432] Fix | Delete
def last: A = {
[433] Fix | Delete
var lst = head
[434] Fix | Delete
for (x <- this)
[435] Fix | Delete
lst = x
[436] Fix | Delete
lst
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
/** Optionally selects the last element.
[440] Fix | Delete
* $orderDependent
[441] Fix | Delete
* @return the last element of this $coll$ if it is nonempty, `None` if it is empty.
[442] Fix | Delete
*/
[443] Fix | Delete
def lastOption: Option[A] = if (isEmpty) None else Some(last)
[444] Fix | Delete
[445] Fix | Delete
/** Selects all elements except the last.
[446] Fix | Delete
* $orderDependent
[447] Fix | Delete
* @return a $coll consisting of all elements of this $coll
[448] Fix | Delete
* except the last one.
[449] Fix | Delete
* @throws `UnsupportedOperationException` if the $coll is empty.
[450] Fix | Delete
*/
[451] Fix | Delete
def init: Repr = {
[452] Fix | Delete
if (isEmpty) throw new UnsupportedOperationException("empty.init")
[453] Fix | Delete
var lst = head
[454] Fix | Delete
var follow = false
[455] Fix | Delete
val b = newBuilder
[456] Fix | Delete
b.sizeHint(this, -1)
[457] Fix | Delete
for (x <- this.seq) {
[458] Fix | Delete
if (follow) b += lst
[459] Fix | Delete
else follow = true
[460] Fix | Delete
lst = x
[461] Fix | Delete
}
[462] Fix | Delete
b.result
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
def take(n: Int): Repr = slice(0, n)
[466] Fix | Delete
[467] Fix | Delete
def drop(n: Int): Repr =
[468] Fix | Delete
if (n <= 0) {
[469] Fix | Delete
val b = newBuilder
[470] Fix | Delete
b.sizeHint(this)
[471] Fix | Delete
b ++= thisCollection result
[472] Fix | Delete
}
[473] Fix | Delete
else sliceWithKnownDelta(n, Int.MaxValue, -n)
[474] Fix | Delete
[475] Fix | Delete
def slice(from: Int, until: Int): Repr = sliceWithKnownBound(math.max(from, 0), until)
[476] Fix | Delete
[477] Fix | Delete
// Precondition: from >= 0, until > 0, builder already configured for building.
[478] Fix | Delete
private[this] def sliceInternal(from: Int, until: Int, b: Builder[A, Repr]): Repr = {
[479] Fix | Delete
var i = 0
[480] Fix | Delete
breakable {
[481] Fix | Delete
for (x <- this.seq) {
[482] Fix | Delete
if (i >= from) b += x
[483] Fix | Delete
i += 1
[484] Fix | Delete
if (i >= until) break
[485] Fix | Delete
}
[486] Fix | Delete
}
[487] Fix | Delete
b.result
[488] Fix | Delete
}
[489] Fix | Delete
// Precondition: from >= 0
[490] Fix | Delete
private[scala] def sliceWithKnownDelta(from: Int, until: Int, delta: Int): Repr = {
[491] Fix | Delete
val b = newBuilder
[492] Fix | Delete
if (until <= from) b.result
[493] Fix | Delete
else {
[494] Fix | Delete
b.sizeHint(this, delta)
[495] Fix | Delete
sliceInternal(from, until, b)
[496] Fix | Delete
}
[497] Fix | Delete
}
[498] Fix | Delete
// Precondition: from >= 0
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function