README.md (1268B)
1 Support an Open Source Developer! :hearts: 2 3 [](https://www.patreon.com/jojolepro) 4 5 # Partial Function 6 A clean way to define function as a set of smaller functions where each has defined start and end bounds. 7 8 ## Partial Function 9 10 Achieves the following: 11 ``` 12 f(x) = { 13 x if 0 <= x < 5 14 x * 2 if 5 <= x <= 10 15 } 16 ``` 17 Expressed as: 18 ```rust 19 let p = PartialFunction::new() 20 .with(0.0, 5.0, Box::new(|x| x )) 21 .with(5.0, 10.0, Box::new(|x| x * 2)) 22 .build(); 23 assert_eq!(p.eval(5.0), Some(10.0)); 24 ``` 25 or even as: 26 ```rust 27 let p = partfn! { 28 [0.0, 5.0]: x -> x, 29 [5.0, 10.0]: x -> x * 2, 30 }; 31 assert_eq!(p.eval(5.0), Some(10.0)); 32 ``` 33 34 ## Lower Partial Function 35 36 Achieves the following: 37 ``` 38 f(x) = { 39 x if 0 <= x < 5 40 x * 2 if 5 <= x 41 } 42 ``` 43 Expressed as: 44 ```rust 45 let f = LowerPartialFunction::new() 46 .with(0.0, Box::new(|x| x )) 47 .with(5.0, Box::new(|x| x * 2)) 48 .build(); 49 assert_eq!(f.eval(5.0), Some(10.0)); 50 ``` 51 or even as: 52 ```rust 53 let f = lowpartfn! { 54 [0.0]: x -> x, 55 [5.0]: x -> x * 2, 56 }; 57 assert_eq!(f.eval(5.0), Some(10.0)); 58 ``` 59 60 ## Adding To Your Project 61 Add the following to your Cargo.toml: 62 ``` 63 partial_function = "0.5.0" 64 ``` 65