This is a basic memoization method for Gleam on the Erlang target, for caching and reusing the results of function calls when possible.
Adding to and reading from the memoization cache incurs some overhead, but in situations that call for it, like dynamic programming problems where the same basic subproblem recurs hundreds or thousands of times, the tradeoff is worthwhile. Always benchmark your code when in doubt.
gleam add rememo
import rememo/ets/memo // This is the recommended implementation to use
import gleam/io
pub fn main() {
// Start the actor that holds the cached values
// for the duration of this block
use cache <- memo.create()
fib(300, cache)
|> io.debug
}
fn fib(n, cache) {
// Check if a value exists for the key n
// Use it if it exists, update the cache if it doesn't
use <- memo.memoize(cache, n)
case n {
1 | 2 -> 1
n -> fib(n - 1, cache) + fib(n - 2, cache)
}
}
Further documentation can be found at https://hexdocs.pm/rememo.
Suggestions and pull requests are welcome.