rememo/otp/memo

This is the memoization implementation that uses gleam/otp/actor.
This is the slower (and original) of the two implementations.

Types

pub opaque type Cache(k, v)

Functions

pub fn create(apply fun: fn(Cache(a, b)) -> c) -> c

Start an actor that holds a memoization cache, and passes the reference to a callback function containing the function you want to memoize.
The actor is shut down after create returns, and the cache is erased with it.

Using create can be done easily with a use expression:

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

Manually look up a value from the memoization cache for a given key.

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

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 and update the cache with the value it returns.

The key can be of any data type, as long as the value the function returns is unique to that particular key.

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(a, b),
  for key: a,
  insert value: b,
) -> Nil

Manually add a key-value pair to the memoization cache.

Search Document