Function rustc_trans::util::common::memoizedExperimental
[-]
[+]
[src]
pub fn memoized<T, U, S, H, F>(cache: &RefCell<HashMap<T, U, H>>, arg: T, f: F) -> U
Memoizes a one-argument closure using the given RefCell containing a type implementing MutableMap to serve as a cache.
In the future the signature of this function is expected to be:
pub fn memoized<T: Clone, U: Clone, M: MutableMap<T, U>>(
cache: &RefCell<M>,
f: &|&: T| -> U
) -> impl |&: T| -> U {
but currently it is not possible.
Example
struct Context { cache: RefCell<HashMap<uint, uint>> } fn factorial(ctxt: &Context, n: uint) -> uint { memoized(&ctxt.cache, n, |n| match n { 0 | 1 => n, _ => factorial(ctxt, n - 2) + factorial(ctxt, n - 1) }) }