I have looked at this question but still don't understand the difference between Iterable and Traversable traits. Can someone explain ?
Traversable is accomplished by the Scala's Collection classes. It is obligatory for Traversable to define foreach method only, as Traversable can inherit all the other methods. The foreach method could traverse all the elements of the Scala's collection class.
Iterable: A base trait for iterable collections. This is a base trait for all Scala collections that define an iterator method to step through one-by-one the collection's elements.
Think of it as the difference between blowing and sucking.
When you have call a Traversable
s foreach
, or its derived methods, it will blow its values into your function one at a time - so it has control over the iteration.
With the Iterator
returned by an Iterable
though, you suck the values out of it, controlling when to move to the next one yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With