You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Generates an infinite stream of Fibonacci numbers.
1023
+
// The generator doesn't keep the array of all numbers.
1024
+
function* fibonacci():IterableIterator<number> {
1025
+
let [a, b] = [0, 1];
1026
+
1027
+
while (true) {
1028
+
yielda;
1029
+
[a, b] = [b, a+b];
1030
+
}
1031
+
}
1032
+
1033
+
function print(n:number) {
1034
+
let i =0;
1035
+
for (const fib infibonacci()) {
1036
+
if (i++===n) break;
1037
+
console.log(fib);
1038
+
}
1039
+
}
1040
+
1041
+
// Print first 10 Fibonacci numbers.
1042
+
print(10);
1043
+
```
1044
+
1045
+
There are libraries that allow working with iterables in a simillar way as with native arrays, by
1046
+
chaining methods like `map`, `slice`, `forEach` etc. See [itiriri](https://www.npmjs.com/package/itiriri) for
1047
+
an example of advanced manipulation with iterables (or [itiriri-async](https://www.npmjs.com/package/itiriri-async) for manipulation of async iterables).
0 commit comments