ジェネレータ



ジェネレータとは?

WikipediaJa:ジェネレータ (プログラミング)

ジェネレータ(英: generator)は、計算機科学の分野において、繰り返し動作の振る舞いを制御するために用いられる特殊なサブルーチンである。ジェネレータは、パラメータを持ち、呼び出し可能で、値の列を持つという点で配列を返す関数と非常に良く似ている。

しかし、すべての要素を一度に計算し配列を構築するのではなく、ジェネレータは一度に一つの値しか生成しない。これによりわずかしかメモリを消費せず、呼び出し側は最初の一部の値を用いて即座に処理を開始できる。そのため、ジェネレータの実装に遅延評価を用いることができる場合もある。

しかし、ジェネエレータは参照透過ではない。簡単に言えば、ジェネレータは関数に類似しているが、イテレータのように振舞う。

WikipediaEn:Generator (computer programming)

In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop.

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.

Generators 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.

WikipediaEn:Coroutine




Tags: generator, ジェネレータ


Last modified : 2013/05/07 11:05:48 UTC