-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.js
117 lines (93 loc) · 2.21 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
function* shopping() {
// stuff on the sidewalk
// walking down the sidewalk
//go into the store with cash
const stuffFromStore = yield 'cash';
//walking to laundry place
const cleanClothes = yield 'laundry';
// walking back home
return [ stuffFromStore, cleanClothes ];
}
// stuff in the store
const gen = shopping();
console.log(gen.next()); // leaving our house
// walked into the store
// walking up and down the aisles...
// purchasing our stuff
console.log(gen.next('groceries')); //leaving the store with groceries.
console.log(gen.next('clean clothes')); //leaving the laundry place with clean clothes.
*/
// ============================================
/*
function* colors() {
yield 'red',
yield 'blue',
yield 'green'
}
const gen = colors();
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());
console.log(gen.next());
const myColors = [];
// for of loop works with generators
for (let color of colors()) {
myColors.push(color);
}
console.log(myColors);
*/
// ==========================================
/*
const testingTeam = {
lead: 'Amanda',
tester: 'Bill',
[Symbol.iterator]: function* () {
yield this.lead;
yield this.tester;
}
};
const engineeringTeam = {
testingTeam,
size: 3,
department: 'Engineering',
lead: 'Jill',
manager: 'Alex',
engineer: 'Dave',
[Symbol.iterator]: function* () {
yield this.lead;
yield this.manager;
yield this.engineer;
yield* this.testingTeam;
}
};
const names = [];
for (let name of engineeringTeam) {
names.push(name);
}
console.log(names);
*/
// ===================================
class Comment {
constructor(content, children) {
this.content = content;
this.children = children;
}
*[Symbol.iterator]() {
yield this.content;
for (let child of this.children) {
yield* child;
}
}
}
const children = [
new Comment('good comment', []),
new Comment('bad comment', []),
new Comment('meh', [])
];
const tree = new Comment('Great post!', children)
const values = [];
for (let value of tree) {
values.push(value);
}
console.log(values);