From 96926a82e592374971f5410bc1051a85f72506f7 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Thu, 27 Feb 2025 00:15:01 +0100 Subject: Flatten amortizations --- src/lib.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5eb8b5b..423f607 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,7 +109,7 @@ impl<'a> Simulation<'a> { println!("Year | Mon | Quota ( Amrtzd + Intrst) | Pending"); for st in self.history.iter() { print!("{st}"); - for update in self.updates.get(st.period) { + for update in flatten_amortizations(self.updates.get(st.period)) { print!(" {update}"); } println!(); @@ -279,3 +279,19 @@ impl SimUpdate { updates } } + +fn flatten_amortizations(updates: Vec<&SimUpdate>) -> Vec { + let mut amortized = 0.; + let mut result = vec![]; + for update in updates { + match update { + SimUpdate::Amortize(n) => { + amortized += n; + } + } + } + if amortized > 0. { + result.push(SimUpdate::Amortize(amortized)); + } + result +} -- cgit v1.2.3