2 ms·
> Seems to me like an iterator solves the lion's share of the problem here. Iterator APIs are indeed aimed at the same kind of problem, but they're not the sam
by ajross 3y ago
> Seems to me like an iterator solves the lion's share of the problem here.
Iterator APIs are indeed aimed at the same kind of problem, but they're not the same solution. And often they're harder to write. If you have a component with a big list of stuff, it's generally easier to write and understand the idea of "iterate over my big list of stuff and emit one item at a time to my consumer" than it is "what state do I need to remember such that when I get called again I can emit the correct next item given the one I just emitted?".
Coroutines are a way of expressing the former. Iterators are the latter. If all you do is write the outer loop, iterators are absolutely just as good. If you need to write the iterator itself, it's more of a discussion.