pub struct BoxConditionalTransformer<T, R> { /* private fields */ }Expand description
BoxConditionalTransformer struct
A conditional transformer that only executes when a predicate is satisfied.
Uses BoxTransformer and BoxPredicate for single ownership semantics.
This type is typically created by calling BoxTransformer::when() and is
designed to work with the or_else() method to create if-then-else logic.
§Features
- Single Ownership: Not cloneable, consumes
selfon use - Conditional Execution: Only transforms when predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic - Implements Transformer: Can be used anywhere a
Transformeris expected
§Examples
§With or_else Branch
use prism3_function::{Transformer, BoxTransformer};
let double = BoxTransformer::new(|x: i32| x * 2);
let negate = BoxTransformer::new(|x: i32| -x);
let conditional = double.when(|x: &i32| *x > 0).or_else(negate);
assert_eq!(conditional.apply(5), 10); // when branch executed
assert_eq!(conditional.apply(-5), 5); // or_else branch executed§Author
Haixing Hu
Implementations§
Source§impl<T, R> BoxConditionalTransformer<T, R>where
T: 'static,
R: 'static,
impl<T, R> BoxConditionalTransformer<T, R>where
T: 'static,
R: 'static,
Sourcepub fn or_else<F>(self, else_transformer: F) -> BoxTransformer<T, R>where
F: Transformer<T, R> + 'static,
pub fn or_else<F>(self, else_transformer: F) -> BoxTransformer<T, R>where
F: Transformer<T, R> + 'static,
Adds an else branch
Executes the original transformer when the condition is satisfied, otherwise executes else_transformer.
§Parameters
else_transformer- The transformer for the else branch, can be:- Closure:
|x: T| -> R BoxTransformer<T, R>,RcTransformer<T, R>,ArcTransformer<T, R>- Any type implementing
Transformer<T, R>
- Closure:
§Returns
Returns the composed BoxTransformer<T, R>
§Examples
§Using a closure (recommended)
use prism3_function::{Transformer, BoxTransformer};
let double = BoxTransformer::new(|x: i32| x * 2);
let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
assert_eq!(conditional.apply(5), 10); // Condition satisfied, execute double
assert_eq!(conditional.apply(-5), 5); // Condition not satisfied, execute negateExamples found in repository?
examples/fn_transformer_ops_demo.rs (line 56)
16fn main() {
17 println!("=== FnTransformerOps Example ===\n");
18
19 // 1. Basic and_then composition
20 println!("1. Basic and_then composition:");
21 let double = |x: i32| x * 2;
22 let to_string = |x: i32| x.to_string();
23 let composed = double.and_then(to_string);
24 println!(
25 " double.and_then(to_string).apply(21) = {}",
26 composed.apply(21)
27 );
28 println!();
29
30 // 2. Chained and_then composition
31 println!("2. Chained and_then composition:");
32 let add_one = |x: i32| x + 1;
33 let double = |x: i32| x * 2;
34 let to_string = |x: i32| x.to_string();
35 let chained = add_one.and_then(double).and_then(to_string);
36 println!(
37 " add_one.and_then(double).and_then(to_string).apply(5) = {}",
38 chained.apply(5)
39 ); // (5 + 1) * 2 = 12
40 println!();
41
42 // 3. compose reverse composition
43 println!("3. compose reverse composition:");
44 let double = |x: i32| x * 2;
45 let add_one = |x: i32| x + 1;
46 let composed = double.compose(add_one);
47 println!(
48 " double.compose(add_one).apply(5) = {}",
49 composed.apply(5)
50 ); // (5 + 1) * 2 = 12
51 println!();
52
53 // 4. Conditional transformation when
54 println!("4. Conditional transformation when:");
55 let double = |x: i32| x * 2;
56 let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
57 println!(" double.when(x > 0).or_else(negate):");
58 println!(" transform(5) = {}", conditional.apply(5)); // 10
59 println!(" transform(-5) = {}", conditional.apply(-5)); // 5
60 println!();
61
62 // 5. Complex composition
63 println!("5. Complex composition:");
64 let add_one = |x: i32| x + 1;
65 let double = |x: i32| x * 2;
66 let triple = |x: i32| x * 3;
67 let to_string = |x: i32| x.to_string();
68
69 let complex = add_one
70 .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
71 .and_then(to_string);
72
73 println!(" add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
74 println!(" transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
75 println!(" transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
76 println!(" transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
77 println!();
78
79 // 6. Type conversion
80 println!("6. Type conversion:");
81 let to_string = |x: i32| x.to_string();
82 let get_length = |s: String| s.len();
83 let length_transformer = to_string.and_then(get_length);
84 println!(
85 " to_string.and_then(get_length).apply(12345) = {}",
86 length_transformer.apply(12345)
87 ); // 5
88 println!();
89
90 // 7. Closures that capture environment
91 println!("7. Closures that capture environment:");
92 let multiplier = 3;
93 let multiply = move |x: i32| x * multiplier;
94 let add_ten = |x: i32| x + 10;
95 let with_capture = multiply.and_then(add_ten);
96 println!(
97 " multiply(3).and_then(add_ten).apply(5) = {}",
98 with_capture.apply(5)
99 ); // 5 * 3 + 10 = 25
100 println!();
101
102 // 8. Function pointers
103 println!("8. Function pointers:");
104 fn double_fn(x: i32) -> i32 {
105 x * 2
106 }
107 fn add_one_fn(x: i32) -> i32 {
108 x + 1
109 }
110 let fn_composed = double_fn.and_then(add_one_fn);
111 println!(
112 " double_fn.and_then(add_one_fn).apply(5) = {}",
113 fn_composed.apply(5)
114 ); // 5 * 2 + 1 = 11
115 println!();
116
117 // 9. Multi-conditional transformation
118 println!("9. Multi-conditional transformation:");
119 let abs = |x: i32| x.abs();
120 let double = |x: i32| x * 2;
121 let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
122 println!(" abs.when(x < 0).or_else(double):");
123 println!(" transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
124 println!(" transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
125 println!(" transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
126 println!();
127
128 println!("=== Example completed ===");
129}Auto Trait Implementations§
impl<T, R> Freeze for BoxConditionalTransformer<T, R>
impl<T, R> !RefUnwindSafe for BoxConditionalTransformer<T, R>
impl<T, R> !Send for BoxConditionalTransformer<T, R>
impl<T, R> !Sync for BoxConditionalTransformer<T, R>
impl<T, R> Unpin for BoxConditionalTransformer<T, R>
impl<T, R> !UnwindSafe for BoxConditionalTransformer<T, R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more