pansa2 5 days ago

> Most iterators provide the ability to walk an entire sequence [...] Calling the iterator again walks the sequence again.

> "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.

2
masklinn 5 days ago

It's more between repeatable iterables and one-shot iterables (of which iterators are a subset, as iterators are iterable). For instance a file is an iterable, but (unless it's seekable and you rewind it) it'll only let you iterate 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).

catlifeonmars 5 days ago

This is the same in JavaScript. There is “pure” Iterable protocol which produces an “impure” Iterator. Interestingly , for loops in JavaScript do not work directly with Iterators; to use in a for loop you must wrap an Iterator in an Iterable

codr7 5 days ago

Java is the same if I remember correctly, always felt like a design failure to me.

masklinn 5 days ago

It's especially dumb for Java, because Iterator could just have been a subtype of Iterable. That's basically what Python does, the iterator protocol requires also being an iterable[0]. And Rust just blanket implements IntoIterator for every Iterator without asking.

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)