Mercurial > lbo > hg > memoize
changeset 47:3ed9875b9139
Add recursive example, rename other examples
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 16 Dec 2020 08:19:04 +0100 |
parents | e68eee272bb3 |
children | d701ff3e25d9 |
files | examples/full_featured.rs examples/recursive.rs examples/test1.rs examples/test2.rs examples/trivial.rs |
diffstat | 5 files changed, 74 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/full_featured.rs Wed Dec 16 08:19:04 2020 +0100 @@ -0,0 +1,32 @@ +use memoize::memoize; +use std::time::{Instant, Duration}; +use std::thread; + +#[derive(Debug, Clone)] +struct ComplexStruct { + s: String, + b: bool, + i: Instant, +} + +#[memoize(TimeToLive: Duration::from_secs(2), Capacity: 123)] +fn hello(key: String) -> ComplexStruct { + println!("hello: {}", key); + ComplexStruct { + s: key, + b: false, + i: Instant::now(), + } +} + +fn main() { + println!("result: {:?}", hello("ABC".to_string())); + println!("result: {:?}", hello("DEF".to_string())); + println!("result: {:?}", hello("ABC".to_string())); //Same as first + thread::sleep(core::time::Duration::from_millis(2100)); + println!("result: {:?}", hello("EFG".to_string())); + println!("result: {:?}", hello("ABC".to_string())); //Refreshed + println!("result: {:?}", hello("EFG".to_string())); //Same as first + println!("result: {:?}", hello("ABC".to_string())); //Same as refreshed + println!("result: {:?}", memoized_original_hello("ABC".to_string())); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/recursive.rs Wed Dec 16 08:19:04 2020 +0100 @@ -0,0 +1,27 @@ +use memoize::memoize; + +#[memoize] +fn fib(n: u64) -> u64 { + if n < 2 { + 1 + } else { + fib(n - 1) + fib(n - 2) + } +} + +#[memoize] +fn fac(n: u64) -> u64 { + if n < 2 { + 1 + } else { + n * fac(n - 1) + } +} + +fn main() { + let fibs = (0..21).map(fib).collect::<Vec<u64>>(); + println!("fib([0,...,20]) = {:?}", fibs); + + let facs = (0..21).map(fac).collect::<Vec<u64>>(); + println!("fac([0,...,20]) = {:?}", facs); +}
--- a/examples/test1.rs Wed Dec 16 08:18:49 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -use memoize::memoize; -use std::time::{Instant, Duration}; -use std::thread; - -#[derive(Debug, Clone)] -struct ComplexStruct { - s: String, - b: bool, - i: Instant, -} - -#[memoize(TimeToLive: Duration::from_secs(2), Capacity: 123)] -fn hello(key: String) -> ComplexStruct { - println!("hello: {}", key); - ComplexStruct { - s: key, - b: false, - i: Instant::now(), - } -} - - - -fn main() { - println!("result: {:?}", hello("ABC".to_string())); - println!("result: {:?}", hello("DEF".to_string())); - println!("result: {:?}", hello("ABC".to_string())); //Same as first - thread::sleep(core::time::Duration::from_millis(2100)); - println!("result: {:?}", hello("EFG".to_string())); - println!("result: {:?}", hello("ABC".to_string())); //Refreshed - println!("result: {:?}", hello("EFG".to_string())); //Same as first - println!("result: {:?}", hello("ABC".to_string())); //Same as refreshed - println!("result: {:?}", memoized_original_hello("ABC".to_string())); -}
--- a/examples/test2.rs Wed Dec 16 08:18:49 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -use memoize::memoize; - -#[memoize] -fn hello(arg: String, arg2: usize) -> bool { - println!("{} => {}", arg, arg2); - arg.len() % 2 == arg2 -} - -fn main() { - // `hello` is only called once here. - assert!(!hello("World".to_string(), 0)); - assert!(!hello("World".to_string(), 0)); - // Sometimes one might need the original function. - assert!(!memoized_original_hello("World".to_string(), 0)); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/trivial.rs Wed Dec 16 08:19:04 2020 +0100 @@ -0,0 +1,15 @@ +use memoize::memoize; + +#[memoize] +fn hello(arg: String, arg2: usize) -> bool { + println!("{} => {}", arg, arg2); + arg.len() % 2 == arg2 +} + +fn main() { + // `hello` is only called once here. + assert!(!hello("World".to_string(), 0)); + assert!(!hello("World".to_string(), 0)); + // Sometimes one might need the original function. + assert!(!memoized_original_hello("World".to_string(), 0)); +}