addString
will take a StringBuilder to add the contents of list into the builder.
Two case fragments can be chained to create a more robust result:
collect
will apply a partial function to all elements of a Traversable
and return a different collection.
collect
will apply a partial function to all elements of a Traversable
and return a different collection. In this exercise, a case fragment is a partial function:
count
will count the number of elements that satisfy a predicate in a Traversable
:
drop
will take the rest of the Traversable
except the number of elements given:
dropWhile
will continually drop elements until a predicate is no longer satisfied:
exists
will determine if a predicate is valid for some members of a Traversable
:
filter
will take out all elements that don't satisfy a predicate.
filter
will take out all elements that don't satisfy a predicate. (An Array
is also Traversable
.)
filterNot
will take out all elements that satisfy a predicate:
find
will locate the first item that matches the predicate p
as Some
, or None
if an element is not found:
flatMap
will not only apply the given function on all elements of a Traversable
, but all elements within the elements and flatten
the results:
flatMap
of Options
will filter out all None
s but keep the Some
s:
flatten
will "pack" all child Traversables
into a single Traversable
:
/:
or foldLeft
will combine an operation starting with a seed and combining from the left.
/:
or foldLeft
will combine an operation starting with a seed and combining from the left. foldLeft
is defined as (seed /: list), where seed is the initial value. Once the fold is established, you provide a function that takes two arguments. The first argument is the running total of the operation, and the second element is the next element of the list.
Given a Traversable (x1, x2, x3, x4)
, an initial value of init
, an operation op
, foldLeft
is defined as: (((init op x1) op x2) op x3) op x4)
:\
or foldRight
will combine an operation starting with a seed and combining from the right.
:\
or foldRight
will combine an operation starting with a seed and combining from the right. Fold right is defined as (list :\ seed), where seed is the initial value. Once the fold is established, you provide a function that takes two elements. The first is the next element of the list, and the second element is the running total of the operation.
Given a Traversable (x1, x2, x3, x4)
, an initial value of init
, an operation op
, foldRight
is defined as: x1 op (x2 op (x3 op (x4 op init)))
forall
will determine if a predicate is valid for all members of a Traversable
:
foreach
will apply a function to all elements of a Traversable
, but unlike the map
function, it will not return anything since the return type is Unit
- an equivalent to a void
return type in Java/C++:
groupBy
will categorize a Traversable
according to a given function and return a map with the results.
groupBy
will categorize a Traversable
according to a given function and return a map with the results. This exercise uses partial function chaining:
hasDefiniteSize
will return true
if the traversable has a finite end, otherwise false
:
head
will return the first element of an ordered collection, or some random element if order is not defined like in a Set
or Map
:
headOption
will return the first element as an Option
of an ordered collection, or some random element if order is not defined.
headOption
will return the first element as an Option
of an ordered collection, or some random element if order is not defined. If a first element is not available, then None
is returned:
init
will return the rest of the collection without the last:
isEmpty
is pretty self-evident:
last
will return the last element of an ordered collection, or some random element if order is not defined:
lastOption
will return the last element as an Option
of an ordered collection, or some random element if order is not defined.
lastOption
will return the last element as an Option
of an ordered collection, or some random element if order is not defined. If a last element is not available, then None
is returned:
map
will apply the given function on all elements of a Traversable
and return a new collection of the result:
mkString
will also take a beginning and ending string to surround the list.:
mkString
will format a Traversable
using a given string as the delimiter:
nonEmpty
is pretty self-evident too:
partition
will split a Traversable
according to a predicate, returning a 2 product Tuple
.
partition
will split a Traversable
according to a predicate, returning a 2 product Tuple
. The left-hand side contains the elements satisfied by the predicate whereas the right hand side contains the rest of the elements. partition
is also defined as (xs filter p, xs filterNot p)
You would choose foldLeft
/reduceLeft
or foldRight
/reduceRight
based on your mathematical goal.
You would choose foldLeft
/reduceLeft
or foldRight
/reduceRight
based on your mathematical goal. One other reason for deciding is performance - foldLeft
generally has better performance since it uses tail recursion. This exercise will either work fine or you will receive a StackOverflowError
:
reduceLeft
is similar to foldLeft
, except that the seed is the head value:
reduceRight
is similar to foldRight
, except that the seed is the last value:
size
provides the size of the traversable:
Given a from
index, and a to
index, slice
will return the part of the collection including from
, and excluding to
:
span
will split a Traversable
according to a predicate, returning a 2 product Tuple
.
span
will split a Traversable
according to a predicate, returning a 2 product Tuple
. span
is also defined as (xs takeWhile p, xs dropWhile p)
splitAt
will split a Traversable
at a position, returning a 2 product Tuple
.
splitAt
will split a Traversable
at a position, returning a 2 product Tuple
. splitAt
is also defined as (xs take n, xs drop n)
There are some methods that take much of the folding work out by providing basic functionality.
There are some methods that take much of the folding work out by providing basic functionality. sum
will add all the elements, product
will multiply, min
would determine the smallest element, and max
the largest:
tail
will return the rest of the collection without the head:
take
is used often with Streams
, since they are also Traversable
:
take
will return the first number of elements given:
takeWhile
will continually accumulate elements until a predicate is no longer satisfied:
toArray
will convert any Traversable
to an Array
, which is a special wrapper around a primitive Java array:
toIndexedSeq
will convert any Traversable
to an IndexedSeq
which is an indexed sequence used in Vectors
and Strings
:
toIterable
will convert any Traversable
to an Iterable
.
toIterable
will convert any Traversable
to an Iterable
. This is a base trait
for all Scala collections that define an iterator method to iterate through the collection's elements:
toList
, as well as other conversion methods such as toSet
and toArray
, will not convert if the collection type is the same:
toList
will convert any Traversable
to a List
.
toMap
will also convert a Set
to a Map
; it should be of parameterized type Tuple2
:
toMap
will convert any Traversable
to a Map
.
toMap
will convert any Traversable
to a Map
. How it's used depends on the original collection; if it's a List
or Seq
, it should be of parameterized type Tuple2
:
toSeq
will convert any Traversable
to a Seq
which is an ordered Iterable
and the superclass to List
, Queue
, and Vector
.
toSeq
will convert any Traversable
to a Seq
which is an ordered Iterable
and the superclass to List
, Queue
, and Vector
. Sequences
provide a method apply for indexing. Indices range from 0 up to the length of a sequence:
toSet
will convert any Traversable
to a Set
which is a collection of unordered, unique values:
toStream
will convert any Traversable* to a
Stream which is a lazy list where elements are evaluated as they are needed:
At the top of the collection hierarchy is the trait Traversable
.
At the top of the collection hierarchy is the trait Traversable
. Its only abstract operation is foreach
:
def foreach[U](f: Elem => U)
Collection classes that implement Traversable
just need to define this method; all other methods can be inherited from Traversable
.
The foreach
method is meant to traverse all elements of the collection, and apply the given operation, f
, to each element. The type of the operation is Elem => U
, where Elem
is the type of the collection's elements and U
is an arbitrary result type. The invocation of f
is done for its side effect only; in fact any function result of f
is discarded by foreach
.
Traversables are the superclass of List
, Array
, Map
, Set
, Stream
and more. The methods involved can be applied to each other in a different type. ++
appends two Traversable
s together.
transpose
will take a traversable of traversables and group them by their position in it's own traversable, e.g.:
((x1, x2),(y1, y2)).transpose = (x1, y1), (x2, y2)
or
((x1, x2, x3),(y1, y2, y3),(z1, z2, z3)).transpose = ((x1, y1, z1), (x2, y2, z2), (x3, y3, z3))
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