January 2020
Intermediate to advanced
532 pages
13h 31m
English
While we are quite happy with the result in the preceding implementation, it feels a little unsatisfactory because we have to write the same code every time we need to memoize a new function. Wouldn't it be nice if the cache is automatically maintained? Realistically, we just need one cache for each function that we want to memoize.
So, let's do it a little differently. The thought is that we should be able to build a higher-order function that takes an existing function and return a memoized version of it. Before we get there, let's first redefine our fib function as an anonymous function, as follows:
fib = n -> begin println("called") return n < 3 ? 1 : fib(n-1) + fib(n-2)end
For now, we ...
Read now
Unlock full access