January 2020
Intermediate to advanced
532 pages
13h 31m
English
In practice, we would probably encounter functions that are more complex than this. For example, the function that requires speed-up probably requires multiple arguments and possibly keyword arguments as well. Our memoize function in the previous section assumes a single argument, so it would not work properly.
A simple way to fix this is illustrated as follows:
function memoize(f) memo = Dict() (args...; kwargs...) -> begin x = (args, kwargs) if haskey(memo, x) return memo[x] else value = f(args...; kwargs...) memo[x] = value return value end endend
The anonymous function being returned now covers any number of positional arguments and keyword arguments as specified in the splatted arguments, ...
Read now
Unlock full access