Generator (computer Programming)
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a generator is a routine that can be used to control the
iteration Iteration is the repetition of a process in order to generate a (possibly unbounded) sequence of outcomes. Each repetition of the process is a single iteration, and the outcome of each iteration is then the starting point of the next iteration. ...
behaviour of a
loop Loop or LOOP may refer to: Brands and enterprises * Loop (mobile), a Bulgarian virtual network operator and co-founder of Loop Live * Loop, clothing, a company founded by Carlos Vasquez in the 1990s and worn by Digable Planets * Loop Mobile, an ...
. All generators are also
iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
s. 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 In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
. Generators can be implemented in terms of more expressive
control flow In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an ''imper ...
constructs, such as
coroutine Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
s or first-class
continuation In computer science, a continuation is an abstract representation of the control state of a computer program. A continuation implements ( reifies) the program control state, i.e. the continuation is a data structure that represents the computati ...
s. Generators, also known as semicoroutines, 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.


Uses

Generators are usually invoked inside loops.The
Icon Programming Language : Icon is a very high-level programming language based on the concept of "goal-directed execution" in which code returns a "success" along with valid values, or a "failure", indicating that there is no valid data to return. The success and failure ...
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 Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
is created that encapsulates the state of the generator routine at its beginning, with arguments bound to the corresponding
parameter A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
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. 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 A stream is a continuous body of water, body of surface water Current (stream), flowing within the stream bed, bed and bank (geography), banks of a channel (geography), channel. Depending on its location or certain characteristics, a stream ...
, 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 A ''list'' is any set of items in a row. List or lists may also refer to: People * List (surname) Organizations * List College, an undergraduate division of the Jewish Theological Seminary of America * SC Germania List, German rugby union ...
, 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 ... /code> 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.


Timeline

Generators first appeared in CLU (1975), were a prominent feature in the string manipulation language
Icon An icon () is a religious work of art, most commonly a painting, in the cultures of the Eastern Orthodox, Oriental Orthodox, and Catholic churches. They are not simply artworks; "an icon is a sacred image used in religious devotion". The most ...
(1977) and are now available in
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
(2001), Python Enhancement Proposals
PEP 255: Simple GeneratorsPEP 289: Generator ExpressionsPEP 342: Coroutines via Enhanced Generators
C#,
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
,
PHP PHP is a general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementation is now produced by The PHP Group ...
,
ECMAScript ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262 ECMAScript is commonly used for client-side scripting o ...
(as of ES6/ES2015), and other languages. In CLU and C#, generators are called ''iterators'', and in Ruby, ''enumerators''.


Lisp

The final
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
standard does not natively provide generators, yet various library implementations exist, such a
SERIES
documented in CLtL2 o
pygen


CLU

A yield statement is used to implement iterators over user-defined data abstractions. string_chars = iter (s: string) yields (char); index: int := 1; limit: int := string$size (s); while index <= limit do yield (string$fetch(s, index)); index := index + 1; end; end string_chars; for c: char in string_chars(s) do ... end;


Icon

Every expression (including loops) is a generator. The language has many generators built-in and even implements some of the logic semantics using the generator mechanism (
logical disjunction In logic, disjunction is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language sentence "it is raining or it is snowing" can be represented in logic using the disjunctive formula R \lor S ...
or "OR" is done this way). Printing squares from 0 to 20 can be achieved using a co-routine by writing: local squares, j squares := create (seq(0) ^ 2) every j := , @squares do if j <= 20 then write(j) else break However, most of the time custom generators are implemented with the "suspend" keyword which functions exactly like the "yield" keyword in CLU.


C

C does not have generator functions as a language construct, but, as they are a subset of coroutines, it is simple to implement them using any framework that implements stackful coroutines, such as libdill. On POSIX platforms, when the cost of
context switching In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes ...
per iteration is not a concern, or full parallelism rather than merely concurrency is desired, a very simple generator function framework can be implemented using
pthreads POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow o ...
and
pipes Pipe(s), PIPE(S) or piping may refer to: Objects * Pipe (fluid conveyance), a hollow cylinder following certain dimension rules ** Piping, the use of pipes in industry * Smoking pipe ** Tobacco pipe * Half-pipe and quarter pipe, semi-circula ...
.


C++

It is possible to introduce generators into C++ using pre-processor macros. The resulting code might have aspects that are very different from native C++, but the generator syntax can be very uncluttered. The set of pre-processor macros defined in this source allow generators defined with the syntax as in the following example: $generator(descent) ; This can then be iterated using: int main(int argc, char* argv[]) Moreover, C++11 allows foreach loops to be applied to any class that provides the begin and end functions. It's then possible to write generator-like classes by defining both the iterable methods (begin and end) and the iterator methods (operator!=, operator++ and operator*) in the same class. For example, it is possible to write the following program: #include int main() A basic range implementation would look like that: class range ;


Perl

Perl does not natively provide generators, but support is provided by th
Coro::Generator
module which uses th
Coro
co-routine framework. Example usage: use strict; use warnings; # Enable generator and yield use Coro::Generator; # Array reference to iterate over my $chars = A'...'Z' # New generator which can be called like a coderef. my $letters = generator ; # Call the generator 15 times. print $letters->(), "\n" for (0..15);


Raku

Example parallel to Icon uses Raku (formerly/aka Perl 6) Range class as one of several ways to achieve generators with the language. Printing squares from 0 to 20 can be achieved by writing: for (0 .. *).map(* ** 2) -> $i However, most of the time custom generators are implemented with "gather" and "take" keywords in a lazy context.


Tcl

In
Tcl TCL or Tcl or TCLs may refer to: Business * TCL Technology, a Chinese consumer electronics and appliance company **TCL Electronics, a subsidiary of TCL Technology * Texas Collegiate League, a collegiate baseball league * Trade Centre Limited ...
8.6, the generator mechanism is founded on named
coroutine Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
s. proc generator # Use a simple 'for' loop to do the actual generation set count enerator # Pull values from the generator until it is exhausted while 1


Haskell

In
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
, with its
lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing). The b ...
model, everything is a generator - every datum created with a non-strict data constructor is generated on demand. For example, countfrom n = n : countfrom (n+1) -- Example use: printing out the integers from 10 to 20. test1 = mapM_ print $ takeWhile (<= 20) $ countfrom 10 primes = 2 : 3 : nextprime 5 where nextprime n , b = n : nextprime (n+2) , otherwise = nextprime (n+2) where b = all ((/= 0).(rem n)) $ takeWhile ((<= n).(^2)) $ tail primes where (:) is a non-strict list constructor, ''cons'', and $ is just a ''"called-with"'' operator, used for parenthesization. This uses the standard adaptor function, takeWhile p [] = [] takeWhile p (x:xs) , p x = x : takeWhile p xs , otherwise = [] which re-fetches values agreeable with a predicate, and stops requesting new values as soon as a non-agreeable one is encountered. The shared storage access is used as a universal mediator in Haskell. List comprehensions can be freely used: test2 = mapM_ print $ takeWhile (<= 20) x <- countfrom 10test3 = mapM_ print x <- takeWhile (<= 20) $ countfrom 10


Racket

Racket provides several related facilities for generators. First, its for-loop forms work with ''sequences'', which are a kind of a producer: (for ( (in-range 10 20) (printf "i = ~s\n" i)) and these sequences are also first-class values: (define 10-to-20 (in-range 10 20)) (for ( 10-to-20 (printf "i = ~s\n" i)) Some sequences are implemented imperatively (with private state variables) and some are implemented as (possibly infinite) lazy lists. Also, new struct definitions can have a property that specifies how they can be used as sequences. But more directly, Racket comes with a generator library for a more traditional generator specification. For example, #lang racket (require racket/generator) (define (ints-from from) (generator () (for ( (in-naturals from) ; infinite sequence of integers from 0 (yield i)))) (define g (ints-from 10)) (list (g) (g) (g)) ; -> '(10 11 12) Note that the Racket core implements powerful continuation features, providing general (re-entrant) continuations that are composable, and also delimited continuations. Using this, the generator library is implemented in Racket.


PHP

The community of PHP implemented generators in PHP 5.5. Details can be found in the origina
Request for Comments: Generators
Infinite Fibonacci sequence: function fibonacci() foreach (fibonacci() as $number) Fibonacci sequence with limit: function fibonacci(int $limit):generator foreach (fibonacci(10) as $number) Any function which contains a yield statement is automatically a generator function.


Ruby

Ruby supports generators (starting from version 1.9) in the form of the built-in Enumerator class. # Generator from an Enumerator object chars = Enumerator.new( A', 'B', 'C', 'Z' 4.times # Generator from a block count = Enumerator.new do , yielder, i = 0 loop end 100.times


Java

Java has had a standard interface for implementing iterators since its early days, and since Java 5, the "foreach" construction makes it easy to loop over objects that provide the java.lang.Iterable interface. (The
Java collections framework The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The collections framework provides both interf ...
and other collections frameworks, typically provide iterators for all collections.) However,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
does not have generators built into the language. This means that creating iterators is often much trickier than in languages with built-in generators, especially when the generation logic is complex. Because all state must be saved and restored every time an item is to be yielded from an iterator, it is not possible to store state in local variables or use built-in looping routines, as when generators are available; instead, all of this must be manually simulated, using object fields to hold local state and loop counters. Even simple iterators built this way tend to be significantly bulkier than those using generators, with a lot of
boilerplate code In computer programming, boilerplate code, or simply boilerplate, are sections of code that are repeated in multiple places with little to no variation. When using languages that are considered ''verbose'', the programmer must write a lot of boile ...
. The original example above could be written in Java 5 as: // Iterator implemented as anonymous class. This uses generics but doesn't need to. for (int i: new Iterable() ) An infinite Fibonacci sequence could also be written in Java 5 as an Iterator: Iterable fibo = new Iterable() ; // this could then be used as... for (int f: fibo) Also an infinite Fibonacci sequence could also be written using Java 8 Stream interface: Iterable myIterable = Stream // Generates Fib sequence .iterate(new Integer[], x -> new Integer[] ) .map(x -> x[0])::iterator; myIterable.forEach(System.out::println); Or get an Iterator from the Java 8 super-interface BaseStream of Stream interface. // Save the iterator of a stream that generates fib sequence Iterator myGenerator = Stream // Generates Fib sequence .iterate(new Integer[], x -> new Integer[] ) .map(x -> x[0]).iterator(); // Print the first 5 elements for (int i = 0; i < 5; i++) System.out.println("done with first iteration"); // Print the next 5 elements for (int i = 0; i < 5; i++) /* Output: 1 1 2 3 5 done with first iteration 8 13 21 34 55 */


C#

An example C# 2.0 generator (the yield is available since C# version 2.0): Both of these examples utilize generics, but this is not required. yield keyword also helps in implementing custom stateful iterations over a collection as discussed in this discussion. // Method that takes an iterable input (possibly an array) // and returns all even numbers. public static IEnumerable GetEven(IEnumerable numbers) It is possible to use multiple yield return statements and they are applied in sequence on each iteration: public class CityCollection : IEnumerable


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


F#

F# provides generators via ''
sequence expression F# (pronounced F sharp) is a functional-first, general purpose, Strong and weak typing, strongly typed, Programming paradigm#Multi-paradigm, multi-paradigm programming language that encompasses functional programming, functional, imperative prog ...
s,'' since version 1.9.1. These can define a sequence (lazily evaluated, sequential access) via seq , a list (eagerly evaluated, sequential access) via ... /code> or an array (eagerly evaluated, indexed access) via seq_ forms_a_sequence_of_squares_of_numbers_from_0_to_14_by_filtering_out_numbers_from_the_range_of_numbers_from_0_to_25.


_Python

Generators_were_added_to_Python
_ Python_may_refer_to: _Snakes *_Pythonidae,_a_family_of_nonvenomous_snakes_found_in_Africa,_Asia,_and_Australia **__''Python''_(genus),_a_genus_of_Pythonidae_found_in_Africa_and_Asia *_Python_(mythology),_a_mythical_serpent _Computing *_Python_(pro_...
_in_version_2.2_in_2001._An_example_generator: from_typing_import_Iterator def_countfrom(n:_int)_->_Iterator nt ____while_True: ________yield_n ________n_+=_1 #_Example_use:_printing_out_the_integers_from_10_to_20. #_Note_that_this_iteration_terminates_normally,_despite #_countfrom()_being_written_as_an_infinite_loop. for_i_in_countfrom(10): ____if_i_<=_20: ________print(i) ____else: ________break #_Another_generator,_which_produces_prime_numbers_indefinitely_as_needed. import_itertools def_primes()_->_Iterator nt ____yield_2 ____n_=_3 ____p_=_[] ____while_True: ________#_If_dividing_n_by_all_the_numbers_in_p,_up_to_and_including_sqrt(n), ________#_produces_a_non-zero_remainder_then_n_is_prime. ________if_all(n_%_f_>_0_for_f_in_itertools.takewhile(lambda_f:_f*f_<=_n,_p)): ____________yield_n ____________p.append(n) ________n_+=_2 In_Python,_a_generator_can_be_thought_of_as_an_iterator_ In_computer_programming,_an_iterator_is_an_object_that_enables_a_programmer_to_traverse_a_container,_particularly_lists._Various_types_of_iterators_are_often_provided_via_a_container's_interface._Though_the_interface_and_semantics_of_a_given_iterat_...
_that_contains_a_frozen_
stack_frame In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mac ...
._Whenever_next()_is_called_on_the_iterator,_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. PEP_380_(implemented_in_Python_3.3)_adds_the_yield_from_expression,_allowing_a_generator_to_delegate_part_of_its_operations_to_another_generator_or_iterable.PEP_380_--_Syntax_for_Delegating_to_a_Subgenerator
/ref>


_Generator_expressions

Python_has_a_syntax_modeled_on_that_of_
list_comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
s,_called_a_generator_expression_that_aids_in_the_creation_of_generators. The_following_extends_the_first_example_above_by_using_a_generator_expression_to_compute_squares_from_the_countfrom_generator_function: squares_=_(n_*_n_for_n_in_countfrom(2)) for_j_in_squares: ____if_j_<=_20: ________print(j) ____else: ________break


_ECMAScript

ECMAScript_6 ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262 ECMAScript is commonly used for client-side scripting o ...
_(a.k.a._Harmony)_introduced_generator_functions. An_infinite_Fibonacci_sequence_can_be_written_using_a_function_generator: function*_fibonacci(limit)_ //_bounded_by_upper_limit_10 for_(const_n_of_fibonacci(10))_ //_generator_without_an_upper_bound_limit for_(const_n_of_fibonacci())_ //_manually_iterating let_fibGen_=_fibonacci(); console.log(fibGen.next().value);_//_1 console.log(fibGen.next().value);_//_1 console.log(fibGen.next().value);_//_2 console.log(fibGen.next().value);_//_3 console.log(fibGen.next().value);_//_5 console.log(fibGen.next().value);_//_8 //_picks_up_from_where_you_stopped for_(const_n_of_fibGen)_


_R

The_iterators_package_can_be_used_for_this_purpose. library(iterators) #_Example_------------------ abc_<-_iter(c('a','b','c')) nextElem(abc)


_Smalltalk

Example_in_ Pharo_Smalltalk: The_
Golden_ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
_generator_below_returns_to_each_invocation_'goldenRatio_next'_a_better_approximation_to_the_Golden_Ratio._ goldenRatio_:=_Generator_on:_ _, _x_y_z_r_, _ x_:=_0. y_:=_1. [__ z_:=_x_+_y. r_:=_(z_/_y)_asFloat. x_:=_y. y_:=_z. g_yield:_r repeat ]. goldenRatio_next. The_expression_below_returns_the_next_10_approximations. Character_cr_join:_((1_to:_10)_collect:_ :dummy_, _ratio_next_. See_more_i
A_hidden_gem_in_Pharo:_Generator


_See_also

*_
List_comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
_for_another_construct_that_generates_a_sequence_of_values *_
Iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
_for_the_concept_of_producing_a_list_one_element_at_a_time *_
Iteratee In functional programming, an iteratee is a composable abstraction for incrementally processing sequentially presented chunks of input data in a purely functional fashion. With iteratees, it is possible to lazily transform how a resource will emi ...
_for_an_alternative *_
Lazy_evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing). The b ...
_for_producing_values_when_needed *_
Corecursion In computer science, corecursion is a type of operation that is dual to recursion. Whereas recursion works analytically, starting on data further from a base case and breaking it down into smaller data and repeating until one reaches a base case, ...
_for_potentially_infinite_data_by_recursion_instead_of_''yield'' *_
Coroutine Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
_for_even_more_generalization_from_subroutine *_
Continuation In computer science, a continuation is an abstract representation of the control state of a computer program. A continuation implements ( reifies) the program control state, i.e. the continuation is a data structure that represents the computati ...
_for_generalization_of_control_flow


_Notes


_References

*_Stephan_Murer,_ Stephen_Omohundro,_David_Stoutamire_and_Clemens_Szyperski:_Iteration_abstraction_in_
Sather Sather is an object-oriented programming language. It originated circa 1990 at the International Computer Science Institute (ICSI) at the University of California, Berkeley, developed by an international team led by Steve Omohundro. It supports ...
.__''ACM_Transactions_on_Programming_Languages_and_Systems'',_18(1):1-15_(1996

{{Authority_control Programming_constructs Articles_with_example_Python_(programming_language)_code Articles_with_example_Haskell_code Articles_with_example_Ruby_code Articles_with_example_C_Sharp_code Articles_with_example_Java_code Articles_with_example_Perl_code Articles_with_example_Tcl_code Articles_with_example_Racket_code Iteration_in_programming Articles_with_example_R_codehtml" ;"title=" ... , ]
that contain code that generates values. For example, seq forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25.


Python

Generators were added to
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
in version 2.2 in 2001. An example generator: from typing import Iterator def countfrom(n: int) -> Iterator nt while True: yield n n += 1 # Example use: printing out the integers from 10 to 20. # Note that this iteration terminates normally, despite # countfrom() being written as an infinite loop. for i in countfrom(10): if i <= 20: print(i) else: break # Another generator, which produces prime numbers indefinitely as needed. import itertools def primes() -> Iterator nt yield 2 n = 3 p = [] while True: # If dividing n by all the numbers in p, up to and including sqrt(n), # produces a non-zero remainder then n is prime. if all(n % f > 0 for f in itertools.takewhile(lambda f: f*f <= n, p)): yield n p.append(n) n += 2 In Python, a generator can be thought of as an
iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
that contains a frozen
stack frame In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mac ...
. Whenever next() is called on the iterator, 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. PEP 380 (implemented in Python 3.3) adds the yield from expression, allowing a generator to delegate part of its operations to another generator or iterable.PEP 380 -- Syntax for Delegating to a Subgenerator
/ref>


Generator expressions

Python has a syntax modeled on that of
list comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
s, called a generator expression that aids in the creation of generators. The following extends the first example above by using a generator expression to compute squares from the countfrom generator function: squares = (n * n for n in countfrom(2)) for j in squares: if j <= 20: print(j) else: break


ECMAScript

ECMAScript 6 ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262 ECMAScript is commonly used for client-side scripting o ...
(a.k.a. Harmony) introduced generator functions. An infinite Fibonacci sequence can be written using a function generator: function* fibonacci(limit) // bounded by upper limit 10 for (const n of fibonacci(10)) // generator without an upper bound limit for (const n of fibonacci()) // manually iterating let fibGen = fibonacci(); console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 2 console.log(fibGen.next().value); // 3 console.log(fibGen.next().value); // 5 console.log(fibGen.next().value); // 8 // picks up from where you stopped for (const n of fibGen)


R

The iterators package can be used for this purpose. library(iterators) # Example ------------------ abc <- iter(c('a','b','c')) nextElem(abc)


Smalltalk

Example in Pharo Smalltalk: The
Golden ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
generator below returns to each invocation 'goldenRatio next' a better approximation to the Golden Ratio. goldenRatio := Generator on: , x y z r , x := 0. y := 1. z := x + y. r := (z / y) asFloat. x := y. y := z. g yield: r repeat goldenRatio next. The expression below returns the next 10 approximations. Character cr join: ((1 to: 10) collect: :dummy , ratio next . See more i
A hidden gem in Pharo: Generator


See also

*
List comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
for another construct that generates a sequence of values *
Iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
for the concept of producing a list one element at a time *
Iteratee In functional programming, an iteratee is a composable abstraction for incrementally processing sequentially presented chunks of input data in a purely functional fashion. With iteratees, it is possible to lazily transform how a resource will emi ...
for an alternative *
Lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing). The b ...
for producing values when needed *
Corecursion In computer science, corecursion is a type of operation that is dual to recursion. Whereas recursion works analytically, starting on data further from a base case and breaking it down into smaller data and repeating until one reaches a base case, ...
for potentially infinite data by recursion instead of ''yield'' *
Coroutine Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
for even more generalization from subroutine *
Continuation In computer science, a continuation is an abstract representation of the control state of a computer program. A continuation implements ( reifies) the program control state, i.e. the continuation is a data structure that represents the computati ...
for generalization of control flow


Notes


References

* Stephan Murer, Stephen Omohundro, David Stoutamire and Clemens Szyperski: Iteration abstraction in
Sather Sather is an object-oriented programming language. It originated circa 1990 at the International Computer Science Institute (ICSI) at the University of California, Berkeley, developed by an international team led by Steve Omohundro. It supports ...
. ''ACM Transactions on Programming Languages and Systems'', 18(1):1-15 (1996

{{Authority control Programming constructs Articles with example Python (programming language) code Articles with example Haskell code Articles with example Ruby code Articles with example C Sharp code Articles with example Java code Articles with example Perl code Articles with example Tcl code Articles with example Racket code Iteration in programming Articles with example R code> ... , /code> that contain code that generates values. For example, seq forms a sequence of squares of numbers from 0 to 14 by filtering out numbers from the range of numbers from 0 to 25.


Python

Generators were added to
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
in version 2.2 in 2001. An example generator: from typing import Iterator def countfrom(n: int) -> Iterator nt while True: yield n n += 1 # Example use: printing out the integers from 10 to 20. # Note that this iteration terminates normally, despite # countfrom() being written as an infinite loop. for i in countfrom(10): if i <= 20: print(i) else: break # Another generator, which produces prime numbers indefinitely as needed. import itertools def primes() -> Iterator nt yield 2 n = 3 p = [] while True: # If dividing n by all the numbers in p, up to and including sqrt(n), # produces a non-zero remainder then n is prime. if all(n % f > 0 for f in itertools.takewhile(lambda f: f*f <= n, p)): yield n p.append(n) n += 2 In Python, a generator can be thought of as an
iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
that contains a frozen
stack frame In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, program stack, control stack, run-time stack, or mac ...
. Whenever next() is called on the iterator, 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. PEP 380 (implemented in Python 3.3) adds the yield from expression, allowing a generator to delegate part of its operations to another generator or iterable.PEP 380 -- Syntax for Delegating to a Subgenerator
/ref>


Generator expressions

Python has a syntax modeled on that of
list comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
s, called a generator expression that aids in the creation of generators. The following extends the first example above by using a generator expression to compute squares from the countfrom generator function: squares = (n * n for n in countfrom(2)) for j in squares: if j <= 20: print(j) else: break


ECMAScript

ECMAScript 6 ECMAScript (; ES) is a JavaScript standard intended to ensure the interoperability of web pages across different browsers. It is standardized by Ecma International in the documenECMA-262 ECMAScript is commonly used for client-side scripting o ...
(a.k.a. Harmony) introduced generator functions. An infinite Fibonacci sequence can be written using a function generator: function* fibonacci(limit) // bounded by upper limit 10 for (const n of fibonacci(10)) // generator without an upper bound limit for (const n of fibonacci()) // manually iterating let fibGen = fibonacci(); console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 1 console.log(fibGen.next().value); // 2 console.log(fibGen.next().value); // 3 console.log(fibGen.next().value); // 5 console.log(fibGen.next().value); // 8 // picks up from where you stopped for (const n of fibGen)


R

The iterators package can be used for this purpose. library(iterators) # Example ------------------ abc <- iter(c('a','b','c')) nextElem(abc)


Smalltalk

Example in Pharo Smalltalk: The
Golden ratio In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their sum to the larger of the two quantities. Expressed algebraically, for quantities a and b with a > b > 0, where the Greek letter phi ( ...
generator below returns to each invocation 'goldenRatio next' a better approximation to the Golden Ratio. goldenRatio := Generator on: , x y z r , x := 0. y := 1. z := x + y. r := (z / y) asFloat. x := y. y := z. g yield: r repeat goldenRatio next. The expression below returns the next 10 approximations. Character cr join: ((1 to: 10) collect: :dummy , ratio next . See more i
A hidden gem in Pharo: Generator


See also

*
List comprehension A list comprehension is a Syntax of programming languages, syntactic construct available in some programming languages for creating a list based on existing list (computing), lists. It follows the form of the mathematical ''set-builder notation'' ( ...
for another construct that generates a sequence of values *
Iterator In computer programming, an iterator is an object that enables a programmer to traverse a container, particularly lists. Various types of iterators are often provided via a container's interface. Though the interface and semantics of a given iterat ...
for the concept of producing a list one element at a time *
Iteratee In functional programming, an iteratee is a composable abstraction for incrementally processing sequentially presented chunks of input data in a purely functional fashion. With iteratees, it is possible to lazily transform how a resource will emi ...
for an alternative *
Lazy evaluation In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing). The b ...
for producing values when needed *
Corecursion In computer science, corecursion is a type of operation that is dual to recursion. Whereas recursion works analytically, starting on data further from a base case and breaking it down into smaller data and repeating until one reaches a base case, ...
for potentially infinite data by recursion instead of ''yield'' *
Coroutine Coroutines are computer program components that generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
for even more generalization from subroutine *
Continuation In computer science, a continuation is an abstract representation of the control state of a computer program. A continuation implements ( reifies) the program control state, i.e. the continuation is a data structure that represents the computati ...
for generalization of control flow


Notes


References

* Stephan Murer, Stephen Omohundro, David Stoutamire and Clemens Szyperski: Iteration abstraction in
Sather Sather is an object-oriented programming language. It originated circa 1990 at the International Computer Science Institute (ICSI) at the University of California, Berkeley, developed by an international team led by Steve Omohundro. It supports ...
. ''ACM Transactions on Programming Languages and Systems'', 18(1):1-15 (1996

{{Authority control Programming constructs Articles with example Python (programming language) code Articles with example Haskell code Articles with example Ruby code Articles with example C Sharp code Articles with example Java code Articles with example Perl code Articles with example Tcl code Articles with example Racket code Iteration in programming Articles with example R code