-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator1.ts
35 lines (34 loc) · 905 Bytes
/
iterator1.ts
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
const fibonacci = (to: number) => {
if (to < 0) {
throw new Error('Unsupported value')
}
to += 2
return {
[Symbol.iterator]: () => {
let step = 1
let prev = 0
let current = 0
return {
next: () => {
step ++
[prev, current] = [current, (prev + current) || 1]
if (step !== to) {
return {
value: {
step: step - 1,
value: prev,
},
done: false
}
}
return {
done: true
}
}
}
}
}
}
for (const v of fibonacci(20)) {
console.log(v)
}