diff --git a/.idea/misc.xml b/.idea/misc.xml
index 89e54d4..9afac53 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -21,6 +21,7 @@
+
diff --git a/.idea/rust.iml b/.idea/rust.iml
index eb9d073..06f6986 100644
--- a/.idea/rust.iml
+++ b/.idea/rust.iml
@@ -82,8 +82,13 @@
+
+
+
+
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 54a3b77..8270e60 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -13,30 +13,12 @@
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -53,9 +35,11 @@
ftoc
+ expensive_closure(intensity)
f_to_c
+ expensive_result.value(intensity)
@@ -97,6 +81,7 @@
+
@@ -106,11 +91,11 @@
false
false
-
-
-
-
-
+
+
+
+
+
@@ -127,22 +112,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
@@ -165,11 +138,12 @@
+
-
+
@@ -183,7 +157,7 @@
-
+
-
+
@@ -216,13 +190,13 @@
-
+
-
+
-
+
@@ -275,11 +249,11 @@
+
-
@@ -304,11 +278,11 @@
-
+
-
-
+
+
@@ -316,7 +290,7 @@
-
+
@@ -324,7 +298,7 @@
-
+
@@ -627,5 +601,12 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/closures/Cargo.toml b/closures/Cargo.toml
new file mode 100644
index 0000000..bef6418
--- /dev/null
+++ b/closures/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "closures"
+version = "0.1.0"
+authors = ["Timothy Warren "]
+edition = "2018"
+
+[dependencies]
diff --git a/closures/src/main.rs b/closures/src/main.rs
new file mode 100644
index 0000000..f616792
--- /dev/null
+++ b/closures/src/main.rs
@@ -0,0 +1,71 @@
+use std::thread;
+use std::time::Duration;
+
+// @TODO: use a hashmap to memoize based on teh argument passed
+// @TODO: make more generic, to accept different types of arguments
+struct Cacher
+ where T: Fn(u32) -> u32
+{
+ calculation: T,
+ value: Option,
+}
+
+impl Cacher
+ where T: Fn(u32) -> u32
+{
+ fn new(calculation: T) -> Cacher {
+ Cacher {
+ calculation,
+ value: None,
+ }
+ }
+
+ fn value(&mut self, arg: u32) -> u32 {
+ match self.value {
+ Some(v) => v,
+ None => {
+ let v = (self.calculation)(arg);
+ self.value = Some(v);
+ v
+ },
+ }
+ }
+}
+
+fn generate_workout(intensity: u32, random_number: u32) {
+ let mut expensive_result = Cacher::new(|num| {
+ println!("calculating slowly...");
+ thread::sleep(Duration::from_secs(2));
+ num
+ });
+
+ if intensity < 25 {
+ println!(
+ "Today do {} pushups!",
+ expensive_result.value(intensity)
+ );
+ println!(
+ "Next, do {} situps!",
+ expensive_result.value(intensity)
+ );
+ } else {
+ if random_number == 3 {
+ println!("Take a break today! Remember to stay hydrated!");
+ } else {
+ println!(
+ "Today, run for {} minutes!",
+ expensive_result.value(intensity)
+ );
+ }
+ }
+}
+
+fn main() {
+ let simulated_user_specified_value = 10;
+ let simulated_random_number = 7;
+
+ generate_workout(
+ simulated_user_specified_value,
+ simulated_random_number
+ );
+}