Generator (computer programming): Difference between revisions

Content deleted Content added
Bender the Bot (talk | contribs)
m Lisp: HTTP → HTTPS for Carnegie Mellon CS, replaced: http://www.cs.cmu.edu/ → https://www.cs.cmu.edu/
Removed "special" from "special routine". It's just a routine. For a thing to be special it must be used in an exceptional and unorthodox manner, which is not the case here, because the full sentence makes clear that this is a routine in the familiar sense.
Line 3:
{{Refimprove|date=July 2007}}
 
In [[computer science]], a '''generator''' is a special [[subroutine|routine]] that can be used to control the [[iteration]] behaviour of a [[control flow#Loops|loop]]. In fact, all generators are [[iterator]]s.<ref>[https://stackoverflow.com/q/1022564 What is the difference between an Iterator and a Generator?]</ref> 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 [[coroutine]]s or first-class [[continuation]]s.<ref>{{cite web