I found this comparison with C# illuminating, trying to make sense of this 
thread.

- It uses duck-typing for its `foreach` loops, allowing them to work with 
anything that has a `GetEnumerator` method.
- All of its collections do have this, by virtue of inheriting from 
`IEnumerable` or `IEnumerable<T>`.
- An "enumerator" has `MoveNext`, `Current`, and `Reset`.
- Generator functions return enumerables, not enumerators. Calling 
`GetEnumerator` on the returned enumerable gives a fresh instance, so a single 
call to a generator function can, by virtue of spawning multiple enumerators, 
allow you to create multiple invocations stepping through the generator 
function.

I believe this is very close to how ES works, as specced:

- `for`-`of` looks for the `@@iterator` symbol, i.e. it works on iterables 
which have a method for getting new iterator instances.
- Arrays and other collections all are iterable, e.g. arrays have an 
`@@iterator` that returns a new iterator for their values (the `values()` 
method does the same).
- Iterators are our friends with the `next` and optional `throw` methods.

Generator functions are a bit different in ES however:

- Each call to a generator function returns a single iterator that is also an 
iterable. Getting `@@iterator` returns itself. So a single call to a generator 
function always creates a single invocation of the generator function to step 
through; you cannot generate multiple invocations.

----

I guess this is kind of the opposite direction Andy and Andreas have been 
going, but it does seem conceptually clearer to me to separate the concept of 
"something that can be iterated" (iterable) from "an instance of that 
iteration" (iterator). The former doesn't have state, usually, whereas the 
latter does. Hrm.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to