rememo/memo

The cache is a Erlang Term Storage database.

Types

pub type Cache(k, v) =
  @internal Set(k, v)

Values

pub fn create(apply fun: fn(Cache(k, v)) -> t) -> t

Make a new memoization cache that will be used for the rest of the inner scope of the provided function. Pass this cache to the function you want to memoize.

This is best used with a use expression:

use cache <- create()
f(a, b, c, cache)
pub fn get(
  from cache: Cache(k, v),
  fetch key: k,
) -> Result(v, Nil)

Manually look up a value from the memoization cache for a given key. Useful if you want to also return intermediate results as well as a final result, for example.

pub fn memoize(
  with cache: Cache(k, v),
  this key: k,
  apply fun: fn() -> v,
) -> v

Look up the value associated with the given key in the memoization cache, and return it if it exists. If it doesn’t exist, evaluate the callback function update the cache with the key and the corresponding value the callback returned, and return that value.

This works well with a use expression:

fn f(a, b, c, cache) {
  use <- memoize(cache, #(a, b, c))
  // function body goes here
}
pub fn set(
  in cache: Cache(k, v),
  for key: k,
  insert value: v,
) -> Nil

Manually add a key-value pair to the memoization cache. Useful if you need to pre-seed the cache with a starting value, for example.

Search Document