#closures #hash-map #cache #recursion #versatile #argument #effectless

memorize

Versatile and fast closure cacher with support for recursive algorithms

2 stable releases

2.0.0 Nov 7, 2023
1.0.0 Oct 30, 2023

#352 in Caching

MIT license

6KB
96 lines

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(&(n - 1)) + r(&(n - 2)),
});
assert_eq!(demo.find(&15), 610)

lib.rs:

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)

Dependencies

~2MB
~27K SLoC