1function* idMaker() {
2 var index = 0;
3 while (true)
4 yield index++;
5}
6
7var gen = idMaker();
8
9console.log(gen.next().value); // 0
10console.log(gen.next().value); // 1
11console.log(gen.next().value); // 2
12console.log(gen.next().value); // 3
13// ...
1function* fibonacci() {
2 var a = yield 1;
3 yield a * 2;
4}
5
6var it = fibonacci();
7console.log(it); // "Generator { }"
8console.log(it.next()); // 1
9console.log(it.send(10)); // 20
10console.log(it.close()); // undefined
11console.log(it.next()); // throws StopIteration (as the generator is now closed)
12
1function* g(){ //or function *g(){}
2 console.log("First");
3 yield 1;
4 console.log("second");
5 yield 2;
6 console.log("third");
7}
8let generator=g();
9generator.next();
10generator.next();
1
2function* expandSpan(xStart, xLen, yStart, yLen) {
3 const xEnd = xStart + xLen;
4 const yEnd = yStart + yLen;
5 for (let x = xStart; x < xEnd; ++x) {
6 for (let y = yStart; y < yEnd; ++y) {
7 yield {x, y};
8 }
9 }
10}
11
12
13//this is code from my mario game I dont think this code is functional
1function* makeRangeIterator(start = 0, end = 100, step = 1) {
2 let iterationCount = 0;
3 for (let i = start; i < end; i += step) {
4 iterationCount++;
5 yield i;
6 }
7 return iterationCount;
8}