Crates.io | partial_function |
lib.rs | partial_function |
version | 0.6.0 |
created_at | 2017-09-27 16:14:45.731706+00 |
updated_at | 2025-02-08 21:56:24.842044+00 |
description | A clean way to define function as a set of subfunctions where each has defined start and end bounds. |
homepage | |
repository | https://github.com/jojolepro/partialfunction |
max_upload_size | |
id | 33671 |
size | 72,028 |
Support an Open Source Developer! :hearts:
A clean way to define function as a set of smaller functions where each has defined start and end bounds.
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));
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));
Add the following to your Cargo.toml:
partial_function = "0.5.0"