Scala: What is the difference between Traversable and Iterable traits in Scala collections?

后端 未结 4 1212
迷失自我
迷失自我 2020-12-04 09:05

I have looked at this question but still don\'t understand the difference between Iterable and Traversable traits. Can someone explain ?

4条回答
  •  难免孤独
    2020-12-04 09:27

    Think of it as the difference between blowing and sucking.

    When you have call a Traversables 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.

提交回复
热议问题