Memoizing generic functions with macros

Earlier, we discussed that generic functions cannot be supported by the memoize function. It would be most awesome if we can just annotate the functions as memoized while they are being defined. For example, the syntax would be like this:

@memoize fib(n) = n < 3 ? 1 : fib(n-1) + fib(n-2)

It turns out that there's already an awesome package called Memoize.jl that does the exact same thing. It is indeed quite convenient:

Here, we can observe the following:

  1. The first call to fib(40) was quite fast already, which is an indication that the cache is utilized.
  2. The second call to fib(40) was almost instant, which means that the result was just a cache lookup.
  3. The third call to fib(39) was almost instant, which means that the result was just a cache lookup.
You should be advised that Memoize.jl does not support mutable data as arguments either. It carries the same problem that we described in the preceding section because it uses the objects' memory addresses as the key to the dictionary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.84.29