-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators.js
More file actions
149 lines (109 loc) · 2.82 KB
/
Copy pathgenerators.js
File metadata and controls
149 lines (109 loc) · 2.82 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// A generator is a function that can be entered and exited multiple times
// With generator, we can run some code, return the value
// and then go right back into the function at the same place that we left it
//Just by adding an asterisks, we create a generator function
// function* numbers() {
//
// }
// //OR
// function *numbers() {
//
// }
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();
gen.next(); // leaving our house
// walked into the store
// walking up and down the aisles..
// purchase our stuff
gen.next('groceries'); // leaving the store with groceries
gen.next('cleanClothes');
console.log(shopping());
function* colors() {
yield 'red';
yield 'blue';
yield 'green';
}
const myColors = [];
for (let color of colors()) {
myColors.push(color);
}
console.log(myColors);
// Generators and for of loops, when used in tandem,
// we can build up objects that will iterate through any
// type of data structure that we can possibly imagine
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;
}
};
// I want to use generators to iterate through the above subject
// I only want to iterate through the employees
// function* TeamIterator(team) {
// yield team.lead;
// yield team.manager;
// yield team.engineer;
// yield* team.testingTeam;
// }
// function* TestingTeamIterator(team) {
// yield team.lead;
// yield team.tester;
// }
const names = [];
for (let name of engineeringTeam) {
names.push(name);
}
console.log(names);
// symbol iterator is a tool that teaches objects how to respond
// to the for of loop
// tree data structure
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);
console.log(tree);
const values = [];
for (let value of tree) {
values.push(value);
}
console.log(values);