memory

为函数提供记忆,不支持递归缓存。

Package Version Hex Docs

gleam add memory
import memory

pub fn factorial(i: Int) -> Int {
  factorial_loop(i, i)
}

fn factorial_loop(i: Int, acc: Int) -> Int {
  case i {
    0 | 1 -> acc
    _ -> factorial_loop(i - 1, acc * { i - 1 })
  }
}

pub fn main() {
  let assert Ok(#(factorial, cache)) = memory.new(factorial, 10)
}

Development

gleam run   # Run the project
gleam test  # Run the tests
Search Document