45 points · by ingve · 1y ago · 16 comments
> "Single-use iterators" break that convention, providing the ability to walk a sequence only once.
This seems similar to the difference between an "iterable" and an "iterator" in Python (and between `IEnumerable` and `IEnumerator` in C#).
A Python list `l = [1, 2, 3]` is an iterable, and you can do `for v in l: print(v)` multiple times. But `iter(l)` is an iterator and `for v in i: print(v)` will only work once.
Basically what Go calls an iterator most other language call an iterable. Because it uses internal iteration, Go doesn't hand out iterators (save through iter.Pull).
At least it's pretty easy to wrap an iterator in JS:
{[Symbol.iterator]: () => it}
[0] which in the rare case you implement an iterator entirely by hand will be a trivial `return self`, so much so that `collection.abc.Iterator` just provides that)- impure iterators are strictly more powerful than pure iterators in that they can be resumed but do not have to be. If I wanted to pick one pattern for consistent behavior across all iterators it would be this one.
Makes sense
> Not all impure iterators can be resumed
How do you mean? In my head resuming an iterator is just partially unrolling a loop.