[erlang-questions] [ANN] memo (memoization server)
Tuncer Ayaz
tuncer.ayaz@REDACTED
Fri Jun 5 17:07:25 CEST 2015
https://github.com/tuncer/memo
memo is a simple memoization server that grew out of noticing a
pattern in code I was refactoring.
Building
--------
$ rebar compile
or
$ erlc +debug_info -o ebin src/*.erl
Testing
-------
$ rebar ct
Dialyzing
---------
Build the PLT
$ rebar build-plt
Run Dialyzer
$ rebar dialyze
By using abbreviated commands, you can also (0) build-plt, (1) compile,
(2) ct (test), and (3) dialyze the code like this:
$ rebar b-p
$ rebar co ct di
This is shorter to type. See 'rebar help' for a description of
abbreviated command support.
Please take note that the Dialyzer commands require the soon to be
released rebar 2.6.0 or current git master.
Using
-----
Assuming the memo application has been started, you can use it as follows:
Instead of
Res = mod:expensive_function(A1, A2, [List1])
you call
Res = memo:call(mod, expensive_function, [A1, A2, [List1]])
or instead of
Res = Fun(A1, A2, [List1])
you call
Res = memo:call(Fun, [A1, A2, [List1]])
or instead of
Res = mod:expensive_function(A1, A2, [List1])
you call
Res = memo:call(fun mod:expensive_function/3, [A1, A2, [List1]])
and any subsequent call will fetch the cached result and avoid the
computation.
This is of course only useful for expensive computations that are
known to produce the same result given same arguments. It's worth
mentioning that your call should be side-effect free, as naturally
those won't be replayed.
The API is simple, and while there was a version that included
memo:forget/0 for clearing the cache, it has since been removed. If
this is something that is seen as useful for more than debugging
purposes, I can add it back.
More information about the erlang-questions
mailing list