Generator (computer programming): Difference between revisions

Content deleted Content added
See also: add more
Arto B (talk | contribs)
Added references to Scheme generators.
Line 7:
Generators are usually [[execution (computers)|invoked]] inside loops. The first time that a generator invocation is reached in a loop, an iterator [[object (computer science)|object]] is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding [[parameter (computer science)|parameter]]s. The generator's body is then executed in the context of that iterator until a special ''yield'' action is encountered; at that time, the value provided with the ''yield'' action is used as the value of the invocation expression. The next time the same generator invocation is reached in a subsequent iteration, the execution of the generator's body is resumed after the ''yield'' action, until yet another ''yield'' action is encountered. In addition to the ''yield'' action, execution of the generator body can also be terminated by a ''finish'' action,
at which time the innermost loop enclosing the generator invocation is terminated.
 
Because generators compute their yielded values only on demand, they are useful for representing sequences that are expensive to compute, or even infinite.
 
In the presence of generators, loop constructs of a language can be reduced into a single loop ... end loop construct; all the usual loop constructs can then be comfortably simulated by using suitable generators in the right way.
Line 31 ⟶ 33:
In Python, a generator can be thought of as an iterator that contains a frozen [[stack frame]]. Whenever the iterator's <code>next()</code> method is called, Python resumes the frozen frame, which executes normally until the next <code>yield</code> statement is reached. The generator's frame is then frozen again, and the yielded value is returned to the caller.
 
In programming languages that support current [[continuation]] capture and resumption, it may be possible to implement generators even if the language itself does not directly provide them. For instance, implementing generators in [[Scheme]] is a [http://xmog.com/scrap/show/5 simple metaprogramming exercise], since the language provides both [[Continuation|call/cc]] and a powerful [[macros]] facility.
Because generators compute their yielded values only on demand, they are useful for representing sequences that are expensive to compute, or even infinite.
 
==See also==
Line 49 ⟶ 51:
** [http://python.org/peps/pep-0289.html PEP 289: ''Generator Expressions'']
** [http://www.python.org/peps/pep-0342.html PEP 342: ''Coroutines via Enhanced Generators'']
* Generators in [[Scheme]]:
** [http://xmog.com/scrap/show/5 Why Java (and almost every other programming language) sucks] by Ed Watkeys
** [http://wmfarr.blogspot.com/2006/08/one-more-example-of-python-generators.html One more example of Python generators in Scheme] by Will Farr
** [http://okmij.org/ftp/Scheme/enumerators-callcc.html General ways to traverse collections in Scheme] by Oleg Kiselyov
** [http://okmij.org/ftp/Computation/Continuations.html#enumerator-stream Towards the best collection traversal interface] by Oleg Kiselyov
** [http://schemecookbook.org/view/Cookbook/IteratorGenerators Schematics Cookbook: Generating Iterators]
** [http://hkn.eecs.berkeley.edu/~dyoo/plt/generator/ Python/Ruby style generators for PLT Scheme]
 
[[Category:Programming constructs]]