Mercurial > lbo > hg > memoize
changeset 7:7974d6e741fc
Show expanded example.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 15 Oct 2020 13:34:30 +0200 |
parents | d3528f4663a3 |
children | 8d44466dbb15 |
files | README.md |
diffstat | 1 files changed, 23 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Thu Oct 15 13:31:43 2020 +0200 +++ b/README.md Thu Oct 15 13:34:30 2020 +0200 @@ -19,4 +19,27 @@ assert!(! memoized_original_hello("World".to_string())); ``` +This is, aside from the `assert`s, expanded into: + +``` +// This is obviously further expanded before compiling. +lazy_static! { + static ref MEMOIZED_MAPPING_HELLO : Mutex<HashMap<String, bool>>; +} + +fn memoized_original_hello(arg: String) -> bool { + arg.len()%2 == 0 +} + +fn hello(arg: String) -> bool { + let mut hm = &mut MEMOIZED_MAPPING_HELLO.lock().unwrap(); + if let Some(r) = hm.get(&arg) { + return r.clone(); + } + let r = memoized_original_hello(arg.clone()); + hm.insert(arg, r.clone()); + r +} +``` + Intentionally not yet on crates.rs.