jargon

Comparison

GeneratorvsIterator pattern

Generator

the function paused in the middle, handed back a value, and carried on from that exact line when asked for the next.

A function that produces a sequence one element at a time, suspending its own execution between them. It is an iterator you write as ordinary straight-line code, which is what makes it so much easier to get right than a hand-written cursor. It is also how a sequence of unbounded or expensive elements becomes practical, because nothing beyond the current element is computed.

Full entry →

Iterator pattern

you walked the collection without knowing whether it was an array, a tree or a paged remote call.

Providing sequential access to the elements of a collection without exposing how it is stored. It is the clearest case of a pattern that became a language feature — foreach, iterators and generators are the pattern built in — which is why it is worth knowing mostly as history and as an interview example. The one live concern it still raises is what happens when the underlying collection changes mid-iteration.

Full entry →

Related comparisons