Skip to content

hunkyjimpjorps/rememo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rememo

Package Version Hex Docs

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.

Contributing and development

Suggestions and pull requests are welcome.

About

Gleam memoization using Erlang Term Storage or actors

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages