3 ms·
This design avoids the inefficiencies and most of the problems of having an?_iterator_, like the classical Java `hasNext`/`moveNext` or C# `MoveNext`/`Current`
by reichstein 3y ago
This design avoids the inefficiencies and most of the problems of having an?_iterator_, like the classical Java `hasNext`/`moveNext` or C# `MoveNext`/`Current` interfaces.
Those iterators require you to keep the state between elements as values, effectively CPS transforming and defunctionalizing the control flow of actually iterating the elements.
This iterator function here is really just a `forEach`/`Each` method where the callback can return a boolean. You can't easily use that to split iteration, say iterate the first five elements, then go do something else, and come back to iterate the rest, doing something completely different.
But that also means it doesn't have to worry, as much, about the iterator going stale because the underlying structure changes (`ConcurrentModificationError`), or not knowing when to clean up, because somebody might call `hasNext`at any later time.
So this is basically just a nicer `forEach` method.