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 qubit_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 executedImplementations§
Source§impl<T, R> BoxConditionalTransformer<T, R>
impl<T, R> BoxConditionalTransformer<T, R>
Sourcepub fn or_else<F>(self, else_transformer: F) -> BoxTransformer<T, R>where
T: 'static,
R: 'static,
F: Transformer<T, R> + 'static,
pub fn or_else<F>(self, else_transformer: F) -> BoxTransformer<T, R>where
T: 'static,
R: 'static,
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
§Returns
Returns a new transformer with if-then-else logic
Examples found in repository?
examples/transformers/fn_transformer_ops_demo.rs (line 60)
20fn main() {
21 println!("=== FnTransformerOps Example ===\n");
22
23 // 1. Basic and_then composition
24 println!("1. Basic and_then composition:");
25 let double = |x: i32| x * 2;
26 let to_string = |x: i32| x.to_string();
27 let composed = double.and_then(to_string);
28 println!(
29 " double.and_then(to_string).apply(21) = {}",
30 composed.apply(21)
31 );
32 println!();
33
34 // 2. Chained and_then composition
35 println!("2. Chained and_then composition:");
36 let add_one = |x: i32| x + 1;
37 let double = |x: i32| x * 2;
38 let to_string = |x: i32| x.to_string();
39 let chained = add_one.and_then(double).and_then(to_string);
40 println!(
41 " add_one.and_then(double).and_then(to_string).apply(5) = {}",
42 chained.apply(5)
43 ); // (5 + 1) * 2 = 12
44 println!();
45
46 // 3. compose reverse composition
47 println!("3. compose reverse composition:");
48 let double = |x: i32| x * 2;
49 let add_one = |x: i32| x + 1;
50 let composed = double.compose(add_one);
51 println!(
52 " double.compose(add_one).apply(5) = {}",
53 composed.apply(5)
54 ); // (5 + 1) * 2 = 12
55 println!();
56
57 // 4. Conditional transformation when
58 println!("4. Conditional transformation when:");
59 let double = |x: i32| x * 2;
60 let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
61 println!(" double.when(x > 0).or_else(negate):");
62 println!(" transform(5) = {}", conditional.apply(5)); // 10
63 println!(" transform(-5) = {}", conditional.apply(-5)); // 5
64 println!();
65
66 // 5. Complex composition
67 println!("5. Complex composition:");
68 let add_one = |x: i32| x + 1;
69 let double = |x: i32| x * 2;
70 let triple = |x: i32| x * 3;
71 let to_string = |x: i32| x.to_string();
72
73 let complex = add_one
74 .and_then(double.when(|x: &i32| *x > 5).or_else(triple))
75 .and_then(to_string);
76
77 println!(" add_one.and_then(double.when(x > 5).or_else(triple)).and_then(to_string):");
78 println!(" transform(1) = {}", complex.apply(1)); // (1 + 1) = 2 <= 5, so 2 * 3 = 6
79 println!(" transform(5) = {}", complex.apply(5)); // (5 + 1) = 6 > 5, so 6 * 2 = 12
80 println!(" transform(10) = {}", complex.apply(10)); // (10 + 1) = 11 > 5, so 11 * 2 = 22
81 println!();
82
83 // 6. Type conversion
84 println!("6. Type conversion:");
85 let to_string = |x: i32| x.to_string();
86 let get_length = |s: String| s.len();
87 let length_transformer = to_string.and_then(get_length);
88 println!(
89 " to_string.and_then(get_length).apply(12345) = {}",
90 length_transformer.apply(12345)
91 ); // 5
92 println!();
93
94 // 7. Closures that capture environment
95 println!("7. Closures that capture environment:");
96 let multiplier = 3;
97 let multiply = move |x: i32| x * multiplier;
98 let add_ten = |x: i32| x + 10;
99 let with_capture = multiply.and_then(add_ten);
100 println!(
101 " multiply(3).and_then(add_ten).apply(5) = {}",
102 with_capture.apply(5)
103 ); // 5 * 3 + 10 = 25
104 println!();
105
106 // 8. Function pointers
107 println!("8. Function pointers:");
108 fn double_fn(x: i32) -> i32 {
109 x * 2
110 }
111 fn add_one_fn(x: i32) -> i32 {
112 x + 1
113 }
114 let fn_composed = double_fn.and_then(add_one_fn);
115 println!(
116 " double_fn.and_then(add_one_fn).apply(5) = {}",
117 fn_composed.apply(5)
118 ); // 5 * 2 + 1 = 11
119 println!();
120
121 // 9. Multi-conditional transformation
122 println!("9. Multi-conditional transformation:");
123 let abs = |x: i32| x.abs();
124 let double = |x: i32| x * 2;
125 let transformer = abs.when(|x: &i32| *x < 0).or_else(double);
126 println!(" abs.when(x < 0).or_else(double):");
127 println!(" transform(-5) = {}", transformer.apply(-5)); // abs(-5) = 5
128 println!(" transform(5) = {}", transformer.apply(5)); // 5 * 2 = 10
129 println!(" transform(0) = {}", transformer.apply(0)); // 0 * 2 = 0
130 println!();
131
132 println!("=== Example completed ===");
133}Trait Implementations§
Source§impl<T, R> Debug for BoxConditionalTransformer<T, R>
impl<T, R> Debug for BoxConditionalTransformer<T, R>
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> UnsafeUnpin 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