Iterators and Generators
Generator Basics
Yield a sequence from a generator function.
generator-basics
A generator function pauses at each `yield`. The caller resumes it when it asks for the next value.
Generator Basics
generator_basics.js
Replay: real traced execution (multi-file project)
const count = 3;
const values = [];
function* numbers(limit) {
let current = 1;
while (current <= limit) {
yield current;
current = current + 1;
}
}
for (const value of numbers(count)) {
values.push(value);
}
const total = values.reduce((sum, value) => sum + value, 0);
console.log("count=" + count);
console.log("values=" + values.join(","));
console.log("total=" + total);
const count = 2;
const values = [];
function* numbers(limit) {
let current = 1;
while (current <= limit) {
yield current;
current = current + 1;
}
}
for (const value of numbers(count)) {
values.push(value);
}
const total = values.reduce((sum, value) => sum + value, 0);
console.log("count=" + count);
console.log("values=" + values.join(","));
console.log("total=" + total);
const count = 5;
const values = [];
function* numbers(limit) {
let current = 1;
while (current <= limit) {
yield current;
current = current + 1;
}
}
for (const value of numbers(count)) {
values.push(value);
}
const total = values.reduce((sum, value) => sum + value, 0);
console.log("count=" + count);
console.log("values=" + values.join(","));
console.log("total=" + total);
count ← 3, values ← (empty)
1const count→ 3 = 3; //@count=2, 52const values→ (empty) = [];current ← 1
1const count = 3; //@count=2, 52const values = [];34function* numbers(limit3) {5 let current→ 1 = 1;6 while (current <= limit) {while (current <= limit)
pass 1 of 34function* numbers(limit) {5 let current = 1;6 while (current1 <= limit3) {7 yield current1;8 current = current + 1;All 3 passes — pass 1 is the card above pass current1 1 2 2 3 3 values ← 1, current ← 2
5 let current = 1;6 while (current <= limit) {7 yield current1;8 current = current→ 2 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value1);14}values this step(empty) → 1valuesvalues ← 1,2, current ← 3
5 let current = 1;6 while (current <= limit) {7 yield current2;8 current = current→ 3 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value2);14}values this step1 → 1,2valuesvalues ← 1,2,3, current ← 4, total ← 6
5 let current = 1;6 while (current <= limit) {7 yield current3;8 current = current→ 4 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value3);14}1516const total→ 6 = values1,2,3.reduce((sum, value) => sum + value, 0);1718console.log("count=" + count3);19console.log("values=" + values1,2,3.join(","));20console.log("total=" + total6);outputcount=3 values=1,2,3 total=6
count ← 2, values ← (empty)
1const count→ 2 = 2;2const values→ (empty) = [];current ← 1
1const count = 2;2const values = [];34function* numbers(limit2) {5 let current→ 1 = 1;6 while (current <= limit) {while (current <= limit)
pass 1 of 24function* numbers(limit) {5 let current = 1;6 while (current1 <= limit2) {7 yield current1;8 current = current + 1;values ← 1, current ← 2
5 let current = 1;6 while (current <= limit) {7 yield current1;8 current = current→ 2 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value1);14}values this step(empty) → 1valueswhile (current <= limit)
pass 2 of 24function* numbers(limit) {5 let current = 1;6 while (current2 <= limit2) {7 yield current2;8 current = current + 1;values ← 1,2, current ← 3, total ← 3
5 let current = 1;6 while (current <= limit) {7 yield current2;8 current = current→ 3 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value2);14}1516const total→ 3 = values1,2.reduce((sum, value) => sum + value, 0);1718console.log("count=" + count2);19console.log("values=" + values1,2.join(","));20console.log("total=" + total3);outputcount=2 values=1,2 total=3
count ← 5, values ← (empty)
1const count→ 5 = 5;2const values→ (empty) = [];current ← 1
1const count = 5;2const values = [];34function* numbers(limit5) {5 let current→ 1 = 1;6 while (current <= limit) {while (current <= limit)
pass 1 of 54function* numbers(limit) {5 let current = 1;6 while (current1 <= limit5) {7 yield current1;8 current = current + 1;All 5 passes — pass 1 is the card above pass current1 1 2 2 3 3 4 4 5 5 values ← 1, current ← 2
5 let current = 1;6 while (current <= limit) {7 yield current1;8 current = current→ 2 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value1);14}values this step(empty) → 1valuesvalues ← 1,2, current ← 3
5 let current = 1;6 while (current <= limit) {7 yield current2;8 current = current→ 3 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value2);14}values this step1 → 1,2valuesvalues ← 1,2,3, current ← 4
5 let current = 1;6 while (current <= limit) {7 yield current3;8 current = current→ 4 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value3);14}values this step1,2 → 1,2,3valuesvalues ← 1,2,3,4, current ← 5
5 let current = 1;6 while (current <= limit) {7 yield current4;8 current = current→ 5 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value4);14}values this step1,2,3 → 1,2,3,4valuesvalues ← 1,2,3,4,5, current ← 6, total ← 15
5 let current = 1;6 while (current <= limit) {7 yield current5;8 current = current→ 6 + 1;9 }10}1112for (const value of numbers(count)) {13 values.push(value5);14}1516const total→ 15 = values1,2,3,4,5.reduce((sum, value) => sum + value, 0);1718console.log("count=" + count5);19console.log("values=" + values1,2,3,4,5.join(","));20console.log("total=" + total15);outputcount=5 values=1,2,3,4,5 total=15