The most elegant Fibonacci function I have found isn't even recursive:
async function* fib(x) {
let x1 = 0;
let x2 = 1;
let i = 0;
while (i < x) {
[x1, x2] = [x2, x1 + x2];
i += 1;
}
yield x1;
}
Generators are great. That being said - they also lend themselves to recursive tasks, since you can execute them 'lazily'.
And traditionally, Fibonacci functions are textbook examples for recursion or memoization.
What I came up with so far doesn't work.
So I was wondering: How would you do a recursive, memoized Fibonacci generator function in JavaScript?