partial_function

[Done] Partial Function library for Rust.
git clone https://git.jojolepro.com/partial_function.git
Log | Files | Refs | README | LICENSE

commit 506351776633259cd288d1edce750fc4bfe9883a
parent 8e9e046ab51253938f4281a9d6ec2a4b86411fe1
Author: Joël Lupien (Jojolepro) <jojolepro@jojolepro.com>
Date:   Sun, 24 May 2020 09:40:49 -0400

Clean up

Diffstat:
MREADME.md | 42+++++++++++++++++++++++++++++++++++++++++-
Msrc/lib.rs | 4++--
Mtests/tests.rs | 48++++++++++++++++++++++++------------------------
3 files changed, 67 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md @@ -1,2 +1,42 @@ +Support an Open Source Developer! <3 +[![Become a patron](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/jojolepro) + # Partial Function -A clean way to define function as a set of subfunctions where each has defined start and end bounds. +A clean way to define function as a set of smaller functions where each has defined start and end bounds. + +## Partial Function + +Achieves the following: +``` +f(x) = { + x if 0 <= x < 5 + x * 2 if 5 <= x <= 10 +} +``` +Expressed as: +```rs +let p = PartialFunction::new() + .with(0.0, 5.0, Box::new(|x| x )) + .with(5.0, 10.0, Box::new(|x| x * 2)) + .build(); +assert_eq!(p.eval(5.0), Some(10.0)); +``` + +## Lower Partial Function + +Achieves the following: +``` +f(x) = { + x if 0 <= x < 5 + x * 2 if 5 <= x +} +``` +Expressed as: +```rs +let f = LowerPartialFunction::new() + .with(0.0, Box::new(|x| x )) + .with(5.0, Box::new(|x| x * 2)) + .build(); +assert_eq!(f.eval(5.0), Some(10.0)); +``` + diff --git a/src/lib.rs b/src/lib.rs @@ -149,10 +149,10 @@ pub struct LowerPartialFunctionBuilder<B, O> { impl<B: PartialOrd, O> LowerPartialFunctionBuilder<B, O> { /// Adds a bounded function bounded between [lower,higher[ of function func. - pub fn with<F: Fn(B) -> O + 'static>(mut self, lower: B, func: F) -> Self { + pub fn with(mut self, lower: B, func: Box<dyn Fn(B) -> O>) -> Self { debug_assert!(self.can_insert(&lower)); let f = LowerBoundedFunction { - func: Box::new(func), + func, lower, }; self.funcs.push(f); diff --git a/tests/tests.rs b/tests/tests.rs @@ -6,37 +6,37 @@ mod tests { use partial_function::*; #[test] fn single() { - let p = PartialFunction::new().with(0.0, 1.0, |x| x).build(); + let p = PartialFunction::new().with(0.0, 1.0, Box::new(|x| x)).build(); assert_eq!(Some(0.5), p.eval(0.5)); } #[test] fn single_start() { - let p = PartialFunction::new().with(0.0, 1.0, |x| x).build(); + let p = PartialFunction::new().with(0.0, 1.0, Box::new(|x| x)).build(); assert_eq!(Some(0.0), p.eval(0.0)); } #[test] fn single_ending() { - let p = PartialFunction::new().with(0.0, 1.0, |x| x).build(); + let p = PartialFunction::new().with(0.0, 1.0, Box::new(|x| x)).build(); assert_eq!(Some(1.0), p.eval(1.0)); } #[test] fn single_nan() { - let p = PartialFunction::new().with(0.0, 1.0, |x| x).build(); + let p = PartialFunction::new().with(0.0, 1.0, Box::new(|x| x)).build(); assert!(p.eval(999.0).is_none()); } #[test] fn dual_start() { let p = PartialFunction::new() - .with(1.0, 2.0, |x| 5.0) - .with(0.0, 1.0, |x| x) + .with(1.0, 2.0, Box::new(|x| 5.0)) + .with(0.0, 1.0, Box::new(|x| x)) .build(); assert_eq!(Some(5.0), p.eval(1.0)); } #[test] fn dual_end() { let p = PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(1.0, 2.0, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(1.0, 2.0, Box::new(|x| 5.0)) .build(); assert_eq!(Some(5.0), p.eval(1.0)); } @@ -44,48 +44,48 @@ mod tests { #[should_panic] fn intersect_start() { PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(-0.5, 0.5, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(-0.5, 0.5, Box::new(|x| 5.0)) .build(); } #[test] #[should_panic] fn intersect_end() { PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(0.5, 2.0, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(0.5, 2.0, Box::new(|x| 5.0)) .build(); } #[test] #[should_panic] fn intersect_inner() { PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(0.4, 0.6, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(0.4, 0.6, Box::new(|x| 5.0)) .build(); } #[test] #[should_panic] fn intersect_outer() { PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(-2.0, 2.0, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(-2.0, 2.0, Box::new(|x| 5.0)) .build(); } #[test] #[should_panic] fn intersect_same() { PartialFunction::new() - .with(0.0, 1.0, |x| x) - .with(0.0, 1.0, |x| 5.0) + .with(0.0, 1.0, Box::new(|x| x)) + .with(0.0, 1.0, Box::new(|x| 5.0)) .build(); } #[test] fn lower_partial_normal() { let f = LowerPartialFunction::new() - .with(0.0, |x| 1) - .with(1.0, |x| 2) + .with(0.0, Box::new(|x| 1)) + .with(1.0, Box::new(|x| 2)) .build(); assert_eq!(f.eval(-1.0), None); assert_eq!(f.eval(0.0), Some(1)); @@ -97,8 +97,8 @@ mod tests { #[test] fn lower_partial_inverse_insert() { let f = LowerPartialFunction::new() - .with(1.0, |x| 2) - .with(0.0, |x| 1) + .with(1.0, Box::new(|x| 2)) + .with(0.0, Box::new(|x| 1)) .build(); assert_eq!(f.eval(-1.0), None); assert_eq!(f.eval(0.0), Some(1)); @@ -111,8 +111,8 @@ mod tests { #[should_panic] fn lower_partial_overlap() { let f = LowerPartialFunction::new() - .with(0.0, |x| 1) - .with(0.0, |x| 2) + .with(0.0, Box::new(|x| 1)) + .with(0.0, Box::new(|x| 2)) .build(); } }