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 known iterables are *Sets*, *Lists*, *Vectors*, *Stacks*, and *Streams*. Iterator has two important methods: hasNext
, which answers whether the iterator has another element available. next
which will return the next element in the iterator.
dropRight
will drop the number of elements from the right.
grouped
will return an fixed sized 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 only be paired.
If two Iterables aren't the same size, then zip
will only zip what can only 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 it's integer index
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assertion and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
This method has been deprecated in favor of macro assumption and will be removed in a future version of ScalaTest. If you need this, please copy the source code into your own trait instead.
Please use 'an [Exception] should be thrownBy { ... }' syntax instead
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expect method has been deprecated. Please replace all invocations of expect with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.
This expectResult method has been deprecated. Please replace all invocations of expectResult with an identical invocation of assertResult instead.