kotliniterablelazy-sequences

Kotlin's Iterable and Sequence look exactly same. Why are two types required?


Both of these interfaces define only one method

public operator fun iterator(): Iterator<T>

Documentation says Sequence is meant to be lazy. But isn't Iterable lazy too (unless backed by a Collection)?


Solution

  • The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T>.