memory
不支持递归,但并发安全。 尾递归函数经过处理依然是尾递归的。
Examples
import gleam/time/timestamp
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 time = fn() { timestamp.system_time() |> timestamp.to_unix_seconds() }
let assert Ok(factorial) = memory.new(factorial)
// 1. 11.648602485656738
// 2. 0.011393308639526367
int.range(0, 2, 0.0, fn(_, _) {
let t = time()
factorial(200_000)
echo time() -. t
})
}