All Topics  
Generator (computer science)

 

   Email Print
   Bookmark   Link






 

Generator (computer science)



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a generator is a special routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 that can be used to control the iteration
Iteration

Iteration means the act of repeating....
 behaviour of a loop
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
. 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.






Discussion
Ask a question about 'Generator (computer science)'
Start a new discussion about 'Generator (computer science)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a generator is a special routine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
 that can be used to control the iteration
Iteration

Iteration means the act of repeating....
 behaviour of a loop
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
. 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
Iterator

In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a Collection , regardless of its specific implementation....
.

History


Generators first appeared in CLU
CLU programming language

CLU is a programming language created at Massachusetts Institute of Technology by Barbara Liskov and her students between 1974 and 1975. It was notable for its use of constructors for abstract data types that included the code that operated on them, a key step in the direction of object-oriented programming ....
 (1975) and are now available in Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, C#, JavaScript
JavaScript

JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard....
, and other languages. (In CLU and C#, generators are called iterators.) Generators are also a prominent feature in the string manipulation language Icon.

Uses


Generators are usually invoked
Execution (computers)

Execution in computer engineering and software engineering is the Process by which a computer or a virtual machine carries out the instructions of a computer program....
 inside loops . The first time that a generator invocation is reached in a loop, an iterator object
Object (computer science)

In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably....
 is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding parameter
Parameter (computer science)

In computer programming, a parameter is a special kind of variable#In_computer_programming that refers to data that a subroutine receives to operate on....
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.

Python


An example Python generator:

def countfrom(n): while True: yield n n += 1

  1. Example use: printing out the integers from 10 to 20.
  2. Note that this iteration terminates normally, despite countfrom being
  3. written as an infinite loop.


for i in countfrom(10): if i <= 20: print(i) else: break

  1. Another generator, which produces prime numbers indefinitely as needed.


def primes: n = 2 p = [] while True: if not any( n % f

0 for f in p ): #This works in Python 2.5+ or with numpy's any yield n p.append( n ) n += 1

In Python, a generator can be thought of as an iterator that contains a frozen stack frame. Whenever the iterator's next method is called, Python resumes the frozen frame, which executes normally until the next yield statement is reached. The generator's frame is then frozen again, and the yielded value is returned to the caller.

Generators can be implemented in terms of more expressive control flow
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
 constructs, such as coroutine
Coroutine

In computer science, coroutines are program components that generalize subroutines to allow multiple entry points for suspending and resuming of execution at certain locations....
s or first-class continuation
Continuation

In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
s.

C#


An example C# 2.0 generator:

// Method that takes an iterable input (possibly an array) // and returns all even numbers. public static IEnumerable GetEven(IEnumerable numbers)



You may even use multiple yield return statements and the compiler will return them in order on each iteration:

public class CityCollection : IEnumerable



Both of these examples utilise Generics, but this is not required. To use the yield keyword, you must be using at least C# version 2.0.

XL


In XL, iterators are the basis of 'for' loops:

import IO = XL.UI.CONSOLE

iterator IntegerIterator (var out Counter : integer; Low, High : integer) written Counter in Low..High is Counter := Low while Counter <= High loop yield Counter += 1

// Note that I needs not be declared, because declared 'var out' in the iterator // An implicit declaration of I as an integer is therefore made here for I in 1..5 loop IO.WriteLn "I=", I

Tcl


In Tcl
Tcl

Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands....
 8.6, the generator mechanism is founded on named coroutine
Coroutine

In computer science, coroutines are program components that generalize subroutines to allow multiple entry points for suspending and resuming of execution at certain locations....
s.

proc generator

  1. Use a simple 'for' loop to do the actual generation
set count [generator ]

  1. Pull values from the generator until it is exhausted
while 1

Other Implementations



Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 does not have continuation
Continuation

In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
 or functionality out of the box, but generators can be implemented using threading. An abstract class that can be subclassed to write generators in Java can be found .

Example of using it: Generator fibonacci = new Generator ; for (int x : fibonacci)

A previous attempt to implement the concept was made, using complex techniques likes bytecode
Bytecode

Bytecode is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software Interpreter as well as being suitable for further compilation into machine language....
 manipulation (specifically, WebObject's ASM
ObjectWeb ASM

The ASM library is a project of the ObjectWeb consortium. It provides a simple API for decomposing, modifying, and recomposing binary Java classes ....
) and the new Java 5 feature of VM Instrumentation. That code was made public .

See also



  • List comprehension
    List comprehension

    A list comprehension is a Syntax of programming languages construct available in some programming languages for creating a list based on existing lists....
     for another construct that generates a sequence of values
  • Iterator
    Iterator

    In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a Collection , regardless of its specific implementation....
     for the concept of producing a list one element at a time
  • Lazy evaluation
    Lazy evaluation

    In computer programming, lazy evaluation is the technique of delaying a computation until such time as the result of the computation is known to be needed....
     for producing values when needed
  • Corecursion
    Corecursion

    In computer science, corecursion is a type of operation that is dual to recursion. Corecursion is typically used to generate infinite data structures....
     for potentially infinite data by recursion instead of yield
  • Coroutine
    Coroutine

    In computer science, coroutines are program components that generalize subroutines to allow multiple entry points for suspending and resuming of execution at certain locations....
     for even more generalization from subroutine
  • Continuation
    Continuation

    In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
     for generalization of control flow