Memo
Generic memoization utility class
Import
_ <- fat.extra.Memo
Constructor
Name | Signature | Brief |
---|---|---|
Memo | (method: Method) | Create a Memo instance for a method |
Prototype members
Name | Signature | Brief |
---|---|---|
asMethod | (): Method | Return a curried version of Memo |
call | (arg: Any): Any | Memoized call; cache and return results |
Example
Memo is useful for optimizing functions by caching results. It stores the outcome of function calls and returns the cached result when the same inputs occur again.
_ <- fat.extra.Memo
fib = (n: Number) -> {
n <= 2 => 1
_ => quickFib(n - 1) + quickFib(n - 2)
}
memoInstance = Memo(fib)
quickFib = memoInstance.asMethod
quickFib(50) # 12586269025
You can now call quickFib
as if you were calling fib
, but with cached results for previously computed inputs.
caveat: may cause memory allocation build-up