generator (computer programming)

aesthetics  →
being  →
complexity  →
database  →
enterprise  →
ethics  →
fiction  →
history  →
internet  →
knowledge  →
language  →
licensing  →
linux  →
logic  →
method  →
news  →
perception  →
philosophy  →
policy  →
purpose  →
religion  →
science  →
sociology  →
software  →
truth  →
unix  →
wiki  →
essay  →
feed  →
help  →
system  →
wiki  →
critical  →
discussion  →
forked  →
imported  →
original  →
generator (computer programming)
[ temporary import ]
please note:
- the content below is remote from Wikipedia
- it has been imported raw for GetWiki
{{other uses|Generator (disambiguation)#Computing}}{{Refimprove|date=July 2007}}In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. In fact, all generators are iterators.What is the difference between an Iterator and a Generator? A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. However, instead of building an array containing all the values and returning them all at once, a generator yields the values one at a time, which requires less memory and allows the caller to get started processing the first few values immediately. In short, a generator looks like a function but behaves like an iterator.Generators can be implemented in terms of more expressive control flow constructs, such as coroutines or first-class continuations.WEB, Kiselyov, Oleg, January 2004, General ways to traverse collections in Scheme,weblink Generators, also known as semicoroutines,BOOK, Anthony Ralston, Encyclopedia of computer science,weblink 11 May 2013, 2000, Nature Pub. Group, 978-1-56159-248-7, are a special case of (and weaker than) coroutines, in that they always yield control back to the caller (when passing a value back), rather than specifying a coroutine to jump to; see comparison of coroutines with generators.


Generators are usually invoked inside loops.The Icon Programming Language utilizes generators to implement its goal directed evaluation. In Icon, generators can be invoked in contexts outside of the normal looping control structures. The first time that a generator invocation is reached in a loop, an iterator object is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding parameters. 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. In more complicated situations, a generator may be used manually outside of a loop to create an iterator, which can then be used in various ways.Because generators compute their yielded values only on demand, they are useful for representing streams, such as sequences that would be expensive or impossible to compute at once. These include e.g. infinite sequences and live data streams.When eager evaluation is desirable (primarily when the sequence is finite, as otherwise evaluation will never terminate), one can either convert to a list, or use a parallel construction that creates a list instead of a generator. For example, in Python a generator g can be evaluated to a list l via l = list(g), while in F# the sequence expression seq { ... } evaluates lazily (a generator or sequence) but [ ... ] evaluates eagerly (a list).In the presence of generators, loop constructs of a language – such as for and while – 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. For example, a ranged loop like for x = 1 to 10 can be implemented as iteration through a generator, as in Python's for x in range(1, 10). Further, break can be implemented as sending finish to the generator and then using continue in the loop.


Generators first appeared in CLU (1975),WEB, Liskov, Barbara, Barbara Liskov, April 1992, A History of CLU,weblink pdf, were a prominent feature in the string manipulation language Icon (1977) and are now available in Python,Python Enhancement Proposals:PEP 255: Simple Generators,PEP 289: Generator Expressions,PEP 342: Coroutines via Enhanced Generators
yield (C# Reference)
Ruby, the later versions of ECMAScript (as of ES6/ES2015) and other languages. In CLU and C#, generators are called iterators, and in Ruby, enumerators.


The final Common Lisp standard does not natively provide generators, yet various library implementations exist, such as SERIES documented in CLtL2 or pygen.


A yield statement is used to implement iterators over user-defined data abstractions.JOURNAL,, Liskov, B., Snyder, A., Atkinson, R., Schaffert, C., Abstraction mechanisms in CLU, Communications of the ACM, 20, 8, 1977, 10.1145/359763.359789, string_chars = iter (s: string) yields (char);
index: int := 1;
limit: int := string$size (s);
while index

- content above as imported from Wikipedia
- "generator (computer programming)" does not exist on GetWiki (yet)
- time: 5:46am EDT - Sun, Aug 25 2019
[ this remote article is provided by Wikipedia ]
LATEST EDITS [ see all ]
Eastern Philosophy
History of Philosophy
M.R.M. Parrott