A part of Concurrency concepts.
Generators are a fundamental part of the Icon programming language [1 ] and were later borrowed by other languages. In Icon generators and co-expressions are related but not identical. (Added by escargo 10/24/2002)
[also called "co-expressions", or, by Donald E. Knuth, "semi-coroutines"]
Generators are big in modern Python.
escargo - Here are some of Ralph Griswold's own words about generators:
In some situations, an expression may be capable of producing more than one result. Consider
sentence := "Store it in the neighboring harbor" find("or", sentence)
Here "or" occurs in sentence at positions 3, 23, and 33. Most programming languages treat this situation by selecting one of the positions, such as the first, as the result of the expression. In Icon, such an expression is a generator and is capable of producing all three positions. The results that a generator produces depend on context. In a situation where only one result is needed, the first is produced, as in
i := find("or", sentence)
which assigns the value 3 to i. If the result produced by a generator does not lead to the success of an enclosing expression, however, the generator is resumed to produce another value. An example is
if (i := find("or", sentence)) > 5 then write(i)
Here the first result produced by the generator, 3, is assigned to i, but this value is not greater than 5 and the comparison operation fails. At this point, the generator is resumed and produces the second position, 23, which is greater than 5. The comparison operation then succeeds and the value 23 is written. Because of the inheritance of failure and the fact that comparison operations return the value of their right argument, this expression can be written in the following more compact form:
write(5 < find("or", sentence))
escargo - Some features of generators (used for goal-directed evaluation with backtracking) are more deeply related to how Icon is interpreted. Iterators (as I see them) are used to iterate over collections. Generators are expressions that can produce zero or more (potentially infinitely more) values that may or may not be related to a collection. Icon has an operator (!) that makes a collection generate its members (strings, sets, lists, and tables all obey !), so it doesn't use iterators itself. (Icon also has an operator (?) that makes a collection generate its members in a pseudorandom way.)