Scala Library Documentation


scala/List.scala
]
sealed abstract
class
List[+a]
extends
Seq[a]a
. This class comes with two implementing case
classes scala.Nil
and scala.::
that
implement the abstract members isEmpty
,
head
and tail
.Method Summary  
def

::
[b >: a](x : b) : List[b]
Add an element 
def

:::
[b >: a](prefix : List[b]) : List[b]
Returns a list resulting from the concatenation of the given
list 
def

apply
(n : Int) : a
Returns the
n th element of this list. The first element
(head of the list) is at position 0. 
def

break
(p : (a) => Boolean) : (List[a], List[a])
Like
span but with the predicate inverted. 
def

count
(p : (a) => Boolean) : Int
Count the number of elements in the list which satisfy a predicate.

def

diff
[b >: a](that : List[b]) : List[b]
Computes the difference between this list and the given list
that . 
override def

drop
(n : Int) : List[a]
Returns the list without its
n first elements.
If this list has less than n elements, the empty list is returned. 
def

dropRight
(n : Int) : List[a]
Returns the list wihout its rightmost
n elements. 
override def

dropWhile
(p : (a) => Boolean) : List[a]
Returns the longest suffix of this list whose first element
does not satisfy the predicate
p . 
def

elements
: Iterator[a]
Returns the elements in the list as an iterator

override def

exists
(p : (a) => Boolean) : Boolean
Tests the existence in this list of an element that satisfies the
predicate
p . 
override final def

filter
(p : (a) => Boolean) : List[a]
Returns all the elements of this list that satisfy the
predicate
p . The order of the elements is preserved. 
override def

find
(p : (a) => Boolean) : Option[a]
Find and return the first element of the list satisfying a
predicate, if any.

override final def

flatMap
[b](f : (a) => Iterable[b]) : List[b]
Applies the given function
f to each element of
this list, then concatenates the results. 
override def

foldLeft
[b](z : b)(f : (b, a) => b) : b
Combines the elements of this list together using the binary
function
f , from left to right, and starting with
the value z . 
override def

foldRight
[b](z : b)(f : (a, b) => b) : b
Combines the elements of this list together using the binary
function
f , from right to left, and starting with
the value z . 
override def

forall
(p : (a) => Boolean) : Boolean
Tests if the predicate
p is satisfied by all elements
in this list. 
override final def

foreach
(f : (a) => Unit) : Unit
Apply the given function
f to each element of this list
(while respecting the order of the elements). 
abstract def

head
: a
Returns this first element of the list.

def

indices
: List[Int]
Creates a list with all indices in the list. This is
equivalent to a call to
List.range(0, xs.length) . 
def

init
: List[a]
Returns the list without its last element.

def

intersect
[b >: a](that : List[b]) : List[b]
Computes the intersection between this list and the given list
that . 
override abstract def

isEmpty
: Boolean
Returns true if the list does not contain any elements.

def

last
: a
Returns the last element of this list.

def

length
: Int
Returns the number of elements in the list.

override final def

map
[b](f : (a) => b) : List[b]
Returns the list resulting from applying the given function
f to each
element of this list. 
def

partition
(p : (a) => Boolean) : (List[a], List[a])
Partition the list in two sublists according to a predicate.

override def

reduceLeft
[b >: a](f : (b, b) => b) : b
Combines the elements of this list together using the binary
operator
op , from left to right 
override def

reduceRight
[b >: a](f : (b, b) => b) : b
Combines the elements of this list together using the binary
operator
op , from right to left 
def

remove
(p : (a) => Boolean) : List[a]
Removes all elements of the list which satisfy the predicate
p . This is like filter with the
predicate inversed. 
def

removeDuplicates
: List[a]
Removes redundant elements from the list. Uses the method
==
to decide if two elements are identical. 
override def

reverse
: List[a]
A list consisting of all elements of this list in reverse order.

def

reverseMap
[b](f : (a) => b) : List[b]
Apply a function to all the elements of the list, and return the
reversed list of results. This is equivalent to a call to
map
followed by a call to reverse , but more efficient. 
def

reverse_:::
[b >: a](prefix : List[b]) : List[b]
Reverse the given prefix and append the current list to that.
This function is equivalent to an application of
reverse
on the prefix followed by a call to ::: , but more
efficient (and tail recursive). 
def

sort
(lt : (a, a) => Boolean) : List[a]
Sort the list according to the comparison function

def

span
(p : (a) => Boolean) : (List[a], List[a])
Returns the longest prefix of the list whose elements all satisfy
the given predicate, and the rest of the list.

def

splitAt
(n : Int) : (List[a], List[a])
Split the list at a given point and return the two parts thus
created.

protected override def

stringPrefix
: String
Defines the prefix of this object's
toString representation. 
abstract def

tail
: List[a]
Returns this list without its first element.

override def

take
(n : Int) : List[a]
Returns the
n first elements of this list, or else the whole
list, if it has less than n elements. 
def

takeRight
(n : Int) : List[a]
Returns the rightmost
n elements from this list. 
override def

takeWhile
(p : (a) => Boolean) : List[a]
Returns the longest prefix of this list whose elements satisfy
the predicate
p . 
override def

toList
: List[a]
Overrides the method in Iterable for efficiency.

def

union
[b >: a](that : List[b]) : List[b]
Computes the union of this list and the given list
that . 
def

zip
[b](that : List[b]) : List[(a, b)]
Returns a list formed from this list and the specified list
that by associating each element of the former with
the element at the same position in the latter.
If one of the two lists is longer than the other, its remaining elements are ignored. 
def

zipAll
[b, c >: a, d >: b](that : List[b], thisElem : c, thatElem : d) : List[(c, d)]
Returns a list formed from this list and the specified list
that by associating each element of the former with
the element at the same position in the latter. 
def

zipWithIndex
: List[(a, Int)]
Returns a list that pairs each element of this list
with its index, counting from 0.

Methods inherited from Seq  
size, concat, ++, isDefinedAt, lastIndexOf, contains, slice, subseq, toArray 
Methods inherited from Collection  
toString 
Methods inherited from Iterable  
findIndexOf, indexOf, /:, :\, copyToBuffer, sameElements, mkString, mkString, addString, addString, copyToArray, projection, hasDefiniteSize 
Methods inherited from PartialFunction  
orElse, andThen 
Methods inherited from Function1  
compose 
Methods inherited from AnyRef  
getClass, hashCode, equals, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized 
Methods inherited from Any  
==, !=, isInstanceOf, asInstanceOf 
Method Details 
override abstract
def
isEmpty : Boolean
true
, iff the list is empty.abstract
def
head : a
Add an element x
at the beginning of this list.
x 
the element to append.x
appended at the beginning.1 :: List(2, 3) = List(2, 3).::(1) = List(1, 2, 3)
Returns a list resulting from the concatenation of the given
list prefix
and this list.
prefix 
the list to concatenate at the beginning of this list.List(1, 2) ::: List(3, 4) = List(3, 4).:::(List(1, 2)) = List(1, 2, 3, 4)
reverse
on the prefix followed by a call to :::
, but more
efficient (and tail recursive).prefix 
the prefix to reverse and then prepend
def
length : Int
List.range(0, xs.length)
.
def
last : a
n
first elements of this list, or else the whole
list, if it has less than n
elements.n 
the number of elements to take.n
first elements of this list.n
first elements.
If this list has less than n
elements, the empty list is returned.n 
the number of elements to drop.n
first elements.n
elements from this list.n 
the number of elements to taken
of the listn
elements.n 
the number of elements to taken
of the listn 
the position at which to splitn
elements, and the other elements.p
.p 
the test predicate.p
.p
.p 
the test predicate.p
.p 
the test predicatep
, and the rest of the list.span
but with the predicate inverted.n
th element of this list. The first element
(head of the list) is at position 0.n 
index of the element to returnn
in this list.f
to each
element of this list.f 
function to apply to each element.[f(a0), ..., f(an)]
if this list is [a0, ..., an]
.map
followed by a call to reverse
, but more efficient.f 
the function to apply to each elements.f
to each element of this list
(while respecting the order of the elements).f 
the treatment to apply to each element.p
. The order of the elements is preserved.p 
the predicate used to filter the list.p
.p
. This is like filter
with the
predicate inversed.p 
the predicate to use to test elementsp
p 
the predicate on which to partitionp
and the list of all elements which do not.
The relative order of the elements in the sublists is the
same as in the original list.
Sort the list according to the comparison function
<(e1: a, e2: a) => Boolean
,
which should be true iff e1
is smaller than
e2
.
lt 
the comparison function<(e1: a, e2: a) => Boolean
.List("Steve", "Tom", "John", "Bob") .sort((e1, e2) => (e1 compareTo e2) < 0) = List("Bob", "John", "Steve", "Tom")
p 
the predicate for which to countp
.p
is satisfied by all elements
in this list.p 
the test predicate.true
iff all elements of this list satisfy the predicate p
.p
.p 
the test predicate.true
iff there exists an element in this list that satisfies the predicate p
.p 
the predicatep
, or None
if none exists.f
, from left to right, and starting with
the value z
.f(... (f(f(z, a_{0}), a_{1}) ...), a_{n})
if the list is
[a_{0}, a_{1}, ..., a_{n}]
.f
, from right to left, and starting with
the value z
.f(a_{0}, f(a_{1}, f(..., f(a_{n}, z)...)))
if the list is [a_{0}, a1, ..., a_{n}]
.op
, from left to rightop 
The operator to applyop(... op(a_{0},a_{1}), ..., a_{n})
if the list has elements
a_{0}, a_{1}, ..., a_{n}
.op
, from right to leftop 
The operator to applya_{0} op (... op (a_{n1} op a_{n})...)
if the list has elements a_{0}, a_{1}, ...,
a_{n}
.f
to each element of
this list, then concatenates the results.f 
the function to apply on each element.f(a_{0}) ::: ... ::: f(a_{n})
if this list is [a_{0}, ..., a_{n}]
.that
by associating each element of the former with
the element at the same position in the latter.
If one of the two lists is longer than the other, its remaining elements are ignored.List({a_{0},b_{0}}, ..., {a_{min(m,n)},b_{min(m,n)}})
when
List(a_{0}, ..., a_{m})
zip List(b_{0}, ..., b_{n})
is invoked.List({a_{0},0}, {a_{1},1}...)
where a_{i}
are the elements of this list.that
by associating each element of the former with
the element at the same position in the latter.that 
list that
may have a different length as the self list.thisElem 
element thisElem
is used to fill up the resulting list if the self list is shorter than
that
thatElem 
element thatElem
is used to fill up the resulting list if that
is shorter than
the self listList({a_{0},b_{0}}, ..., {a_{n},b_{n}}, {elem,b_{n+1}},
..., {elem,b_{m}})
when [a_{0}, ..., a_{n}] zip
[b_{0}, ..., b_{m}]
is
invoked where m > n
.that
.that 
the list of elements to add to the list.that
.that
.that 
the list of elements to remove from this list.that
.that
.that 
the list to intersect.that
.==
to decide if two elements are identical.protected override
def
stringPrefix : String
toString
representation.
Scala Library Documentation

