The next trait from the top in the collections hierarchy is Iterable
.
The next trait from the top in the collections hierarchy is Iterable
. All methods in this trait are defined in terms of an abstract method, iterator
, which yields the collection's elements one by one. The foreach
method from trait Traversable
is implemented in Iterable
in terms of iterator
. Here is the actual implementation:
def foreach[U](f: Elem => U): Unit = { val it = iterator while (it.hasNext) f(it.next()) }
Quite a few subclasses of Iterable
override this standard implementation of foreach
in Iterable
, because they can provide a more efficient implementation. Remember that foreach
is the basis of the implementation of all operations in Traversable
, so its performance matters.
Some common iterables are Set
, List
, Vector
, Stacks
and Stream
. Iterator has two important methods: hasNext
, which answers whether the iterator has another element available, and next
which returns the next element in the iterator.
dropRight
will drop a specified number of elements from the right:
grouped
will return fixed-size Iterable
chucks of an Iterable
:
sameElements
will return true if the two Iterables
produce the same elements in the same order:
If two Iterables aren't the same size, then zip
will only zip what can be paired.
If two Iterables aren't the same size, then zip
will only zip what can be paired.
e.g. Iterable(x1, x2, x3) zip Iterable(y1, y2)
will return ((x1, y1), (x2, y2))
:
sliding
will return an Iterable
that shows a sliding window of an Iterable
.
sliding
can take the size of the window as well the size of the step during each iteration:
takeRight
is the opposite of 'take' in Traversable
.
takeRight
is the opposite of 'take' in Traversable
. It retrieves the last elements of an Iterable
:
If two Iterables
aren't the same size, then zipAll
can provide fillers for what it couldn't find a complement for.
If two Iterables
aren't the same size, then zipAll
can provide fillers for what it couldn't find a complement for.
e.g. Iterable(x1, x2, x3) zipAll (Iterable(y1, y2), x, y)
will return ((x1,y1), (x2, y2), (x3, y)))
:
zip
will stitch two iterables into an iterable of pairs of corresponding elements from both iterables.
zip
will stitch two iterables into an iterable of pairs of corresponding elements from both iterables.
e.g. Iterable(x1, x2, x3) zip Iterable(y1, y2, y3)
will return ((x1, y1), (x2, y2), (x3, y3))
:
zipWithIndex
will zip an Iterable
with its integer index:
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