November 2017
Intermediate to advanced
386 pages
9h 22m
English
We will work with the Fibonacci function we mentioned, which is a simple case: it receives a single numeric parameter. The function, as we saw it, was the following:
function fib(n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fib(n - 2) + fib(n - 1); }}
The solution we did there was general in concept, but particularly in its implementation: we had to directly modify the code of the function in order to take advantage of said memoization. Now we should look into a way of doing it automatically, in the same fashion as with other wrapped functions. The solution would be a memoize() function that wraps any other function, to apply memoization:
const memoize = fn => { let cache = {}; return x => ...