my-solutions/leetcode/2648-generate-fibonacci-sequence/main.ts

20 lines
379 B
TypeScript
Raw Permalink Normal View History

2024-02-07 21:48:43 +05:00
/**
* const gen = fibGenerator();
* gen.next().value; // 0
* gen.next().value; // 1
*/
function* fibGenerator(): Generator<number, any, number> {
let a = 0;
let b = 1;
let iter = 0;
while (true) {
if (iter < 2) {
yield iter;
iter++;
} else {
yield a + b;
[a, b] = [b, a + b];
}
}
};