8 releases (5 breaking)
new 0.6.0 | Feb 8, 2025 |
---|---|
0.5.0 | May 24, 2020 |
0.4.0 | Aug 9, 2018 |
0.3.0 | Aug 8, 2018 |
0.1.2 | Sep 27, 2017 |
#277 in Rust patterns
133 downloads per month
Used in 4 crates
(2 directly)
21KB
136 lines
Support an Open Source Developer! ♥️
Partial Function
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:
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));
or even as:
let p = partfn! {
[0.0, 5.0]: x -> x,
[5.0, 10.0]: x -> x * 2,
};
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:
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));
or even as:
let f = lowpartfn! {
[0.0]: x -> x,
[5.0]: x -> x * 2,
};
assert_eq!(f.eval(5.0), Some(10.0));
Adding To Your Project
Add the following to your Cargo.toml:
partial_function = "0.5.0"
Dependencies
~210–650KB
~15K SLoC