A tiny memoization library built on FLEXI-STREAMS and CL-STORE. This uses implementation-specific (currently supports SBLC and Clozure CL) thread safe hashes to ensure thread safe memoization.
The code is available under the GNU General Public License.
Source:
http://github.com/eschulte/memoize.
This package lets the particular lisp distribution handle the creation of thread-safe hashes to hold memoized data, and lets cl-store and flexi-streams do all the heavy lifting of hashing arbitrary arguments.
On these shoulders it builds a simple hashing system which is used
to hash function output by function arguments. The API consists of
memoize
which can be called to memoize a function and
un-memoize
which can be called to un-memoize a function. All
saved memoized data is available through the *memoized-data*
array which may be persisted using cl-store.
* (defun foo (it) (format t "~S~%" it) (random 2400))
FOO
* (memoize #'foo)
#<CLOSURE (LAMBDA (&REST ARGS) :IN MEMOIZE) {1002BC373B}>
* (foo 2)
2
1324
* (foo 2)
1324
* *memoized-data*
((FOO . #<HASH-TABLE :TEST EQL :COUNT 1 {1002BC3653}>))
* *memoized-functions*
((FOO . #<FUNCTION FOO {1002BAE3BB}>))
* (un-memoize 'foo)
NIL
* *memoized-functions*
NIL
* *memoized-data*
NIL
* (foo 2)
2
2147
* (foo 2)
2
1573
Note: currently only SBCL and Clozure CL are supported, but other distributions w/thread-safe hashes should be easy to add.
TODO: Support memoization of generic functions (i.e., defmethod).
[Special variable]
*memoized-data*
Alist of memoization hashes keyed by function name.
[Function]
memoize func &key key if-memoized => result
Update FUNC so all future calls are memoized. Optionally the output of the function specified by :KEY will be used for hashing and equality tests in memoization. Keyword argument :IF-MEMOIZED may be one of :ERROR (the default) which raises an error if FUNC is already memoized or :REPLACE which will replace the memoized version of FUNC deleting any existing memoized data.
[Function]
un-memoize func-name => result
Un-memoize the function identified by the symbol FUNC-NAME.
This documentation was prepared with a hacked up version of DOCUMENTATION-TEMPLATE.