|
|
|
|
|
An optional second argument to memoize provides a list of initial values, each of the form x => v, where v is the value to be provided for the argument x.
Alternatively, values can be provided after defining the memoized function using the syntax f x = v. A slightly more efficient implementation of the above would be
|
|
|
The function memoize operates by constructing a MutableHashTable, in which the arguments are used as keys for accessing the return value of the function. This mutable hash table can be obtained using the function memoizeValues, as follows.
|
That hash table can be replaced by an empty one with the function memoizeClear.
|
|
Warning: the new function created by memoize will save references to all arguments and values it encounters, and this will often prevent those arguments and values from being garbage-collected as soon as they might have been. If the arguments are implemented as mutable hash tables (modules, matrices and rings are implemented this way) then a viable strategy is to stash computed results in the arguments themselves. See also CacheTable.
The object memoize is a method function.