generatorの概念
generator(という概念)とは、繰り返しを簡単に・見通し良く記述する仕組みである.
generatorの利用
generatorを利用することによって
- 繰り返しオブジェクト(イテレータ)の容易な作製
- 見通しの良い非同期処理コード記述
が可能になる.
generatorに関わる用語
- generator関数
- generator(generatorオブジェクト)
- iterator
用語の説明まとめ
- generator関数: generatorのコンストラクタ関数
- generator(generatorオブジェクト): generator関数のinstance。Iteratorでもあり、またIterable interfaceでもある
generator関数とは
Thus the function call GeneratorFunction (…) is equivalent to the object creation expression new GeneratorFunction (…) with the same arguments.
ジェネレーターの説明
ジェネレーターは処理を抜け出すことも後から復帰することもできる関数です。ジェネレーターのコンテキスト (変数の値)は復帰しても保存されます。
function* - JavaScript | MDN
ECMAScript2016 language specification該当部分
25 Control Abstraction Objects …
25.2 GeneratorFunction Objects
Generator Function objects are functions that are usually created by evaluating GeneratorDeclaration, GeneratorExpression, and GeneratorMethod syntactic productions. They may also be created by calling the %GeneratorFunction% intrinsic.
https://www.ecma-international.org/ecma-262/7.0/#sec-generatorfunction-objects
…
25.3 Generator Objects
A Generator object is an instance of a generator function and conforms to both the Iterator and Iterable interfaces.
https://www.ecma-international.org/ecma-262/7.0/#sec-generator-objects
yeild
yeild*
yeild*は、子プロセスのyeildを親プロセスでyeildするための仕組み.
入門以上
Node.js streamとの組み合わせ
あまりwebに文献はないが、Node.js readable-streamとすこぶる相性がよい気がする.
read()の中でgeneratorを使えば、read()呼び出しのたびに1つずつyieldできるから.
再帰させてyield*と組み合わせたら、とても良いコードがかけた.
generatorとか、iteratorとか
要は、状態を抱え込む反復可能オブジェクト.
関数型とは相いれないものなのかなーと。関数型に興味がわいてきた.