-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterables.js
50 lines (46 loc) · 1.26 KB
/
iterables.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
module.exports = {
run: () => {
const print = (all, limit = 10) => {
let counter = 0
for (let e of all) {
console.log(e)
counter += 1
if (counter > limit) {
break;
}
}
console.log("\n")
}
// Style 1
let elements1 = {}
elements1[Symbol.iterator] = () => {
let first = 1, second = 1
return {
next: () => {
let result = { value: first, done: false }
let third = first + second
first = second
second = third
return result
}
}
}
console.log("Style 1 Output:\n")
print(elements1)
// Style 2
let elements2 = {}
elements2[Symbol.iterator] = function* () {
let first = 1, second = 1, third = 0
yield first
yield second
while (true) {
third = first + second
first = second
second = third
yield third
}
}
console.log("Style 2 Output:\n")
print(elements2)
}
}