20 lines
379 B
TypeScript
20 lines
379 B
TypeScript
|
/**
|
||
|
* 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];
|
||
|
}
|
||
|
}
|
||
|
};
|