Expand description
Cache the return values of an effectless closure in a hashmap
Inspired by the closure_cacher
crate, but attempts to provide a more
versatile implementation.
use memorize::{cached, Cache};
let demo = cached(|arg, _| arg * 2);
assert_eq!(demo.find(&7), 14);
The second argument is a callback, it can be used for recursion.
use memorize::{cached, Cache};
let demo = cached(|arg, r| match arg {
1 | 2 => 1,
n => r.r(&(n - 1)) + r.r(&(n - 2)),
});
assert_eq!(demo.find(&15), 610)
Structs§
- Recur
- A wrapper around the recursive self-handle to keep
rust-analyzer
from hanging. See rust-analyzer#11063
Traits§
- Cache
- A cache that can be polled for a value with its borrowed form.
Functions§
- cached
- Memoize a function, returning an opaque object that remembers arguments and return values.