Memoisation

datastructures provides simple ways to cache return values of pure functions.

Memoisation with HashMap

A function MemoizeFunction returns a function which behaves the same as function, except that it caches the return value of function. The cache can be flushed by calling .

This function does not promise to never call function more than once for any input -- values may be removed if the cache gets too large, or GAP chooses to flush all caches, or if multiple threads try to calculate the same value simultaneously.

The optional second argument is a record which provides a number of configuration options. The following options are supported. flush (default true) If this is true, the cache is emptied whenever is called. contract (defaults to ) A function that is called on the arguments given to function. If this function returns false, then errorHandler is called. errorHandler (defaults to none) A function to be called when an input that does not fulfil contract is passed to the cache.