-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.js
64 lines (56 loc) · 1.55 KB
/
generators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// tedious to write iterators that yield one value with each call to next
// function(*) is suspended after each yielded value
'use strict';
function* rangeGenerator(from, to) {
for(let i=from; i < to; i++)
yield i;
}
const rangeIter= rangeGenerator(1, 10)
console.log(rangeIter.next())
console.log(rangeIter.next())
rangeIter.return(undefined)
// const myObject = { * myGenerator(. . .) { . . . }, . . . }
// Syntactic sugar for myGenerator: function* (. . .) { . . . }
function* arrayGen(arr) {
for(let ele of arr) yield ele;
}
let arr = [1,2,[1,[2]]]
let arrayIter = arrayGen(arr);
console.log(arrayIter.next())
console.log(arrayIter.next())
console.log(arrayIter.next())
console.log(arrayIter.next())
function* flatArrGenerator(arr) {
for(let ele of arr) {
if(Array.isArray(ele)) {
// yield* ele;
yield* flatArrGenerator(ele); // yield* means give iterable of ...
} else {
yield ele;
}
}
}
console.log('FlatArray')
arrayIter = flatArrGenerator(arr);
console.log(arrayIter.next())
console.log(arrayIter.next())
console.log(arrayIter.next())
console.log(arrayIter.next())
console.log(arrayIter.next())
// Generators as consumers
console.log('consumers')
function* sumAccum() {
let sum =0;
// let i=0;
while (true){
// console.log(i++)
let nextVal = yield sum;
sum += nextVal
}
}
let accum = sumAccum()
console.log(accum.next()) // advance to the first yield
console.log(accum.next(1))
console.log(accum.next(2))
console.log(accum.next(3))
accum.return()