Lists are immutable:
Lists can be accessed by position:
Lists can be easily created:
foldLeft
is like reduce
, but with an explicit starting value:
You can create a list from a range:
Functions over lists can use ()
instead of {}
:
Lists can be accessed via head
, headOption
and tail
.
Lists can be accessed via head
, headOption
and tail
. Accessing a list via head
is unsafe and may result in a IndexOutOfBoundsException
.
Nil lists are identical, even of different types:
Lists can be reduced with a mathematical operation:
Lists reuse their tails:
==
tests equality (same content):
Scala Lists are quite similar to arrays, which means all the elements of a list have the same type - but there are two important differences.
Scala Lists are quite similar to arrays, which means all the elements of a list have the same type - but there are two important differences. First, lists are immutable, which means elements of a list cannot be changed by assignment. Second, lists represent a linked list whereas arrays are flat. The type of a list that has elements of type T
is written as List[T]
.
eq
tests identity (same object):
Lists have many useful utility methods:
Functions over lists can use _ as shorthand:
The parameterless execute method has been deprecated and will be removed in a future version of ScalaTest. Please invoke execute with empty parens instead: execute().
The trap method is no longer needed for demos in the REPL, which now abreviates stack traces, and will be removed in a future version of ScalaTest