memory
为函数提供记忆,不支持递归缓存。
缓存会单独开启一个进程,如果在并发场景使用的话上一个函数调用会阻塞下一个。
原本并行调用的函数经过处理后变成了串行调用。
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