pub struct BoxConditionalBiTransformer<T, U, R> { /* private fields */ }Expand description
BoxConditionalBiTransformer struct
A conditional bi-transformer that only executes when a bi-predicate is
satisfied. Uses BoxBiTransformer and BoxBiPredicate for single
ownership semantics.
This type is typically created by calling BoxBiTransformer::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 bi-predicate returns
true - Chainable: Can add
or_elsebranch to create if-then-else logic - Implements BiTransformer: Can be used anywhere a
BiTransformeris expected
§Examples
§With or_else Branch
use qubit_function::{BiTransformer, BoxBiTransformer};
let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0).or_else(multiply);
assert_eq!(conditional.apply(5, 3), 8); // when branch executed
assert_eq!(conditional.apply(-5, 3), -15); // or_else branch executed§Author
Haixing Hu
Implementations§
Source§impl<T, U, R> BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> BoxConditionalBiTransformer<T, U, R>
Sourcepub fn or_else<F>(self, else_transformer: F) -> BoxBiTransformer<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
F: BiTransformer<T, U, R> + 'static,
pub fn or_else<F>(self, else_transformer: F) -> BoxBiTransformer<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
F: BiTransformer<T, U, 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 bi-transformer with if-then-else logic
Examples found in repository?
examples/transformers/fn_bi_transformer_ops_demo.rs (line 49)
19fn main() {
20 println!("=== FnBiTransformerOps Demo ===\n");
21
22 // Example 1: Basic and_then composition
23 println!("1. Basic and_then composition:");
24 let add = |x: i32, y: i32| x + y;
25 let double = |x: i32| x * 2;
26
27 let composed = add.and_then(double);
28 let result = composed.apply(3, 5);
29 println!(" (3 + 5) * 2 = {}", result);
30 println!();
31
32 // Example 2: Type conversion and_then
33 println!("2. Type conversion and_then:");
34 let multiply = |x: i32, y: i32| x * y;
35 let to_string = |x: i32| format!("Result: {}", x);
36
37 let composed = multiply.and_then(to_string);
38 let result = composed.apply(6, 7);
39 println!(" 6 * 7 = {}", result);
40 println!();
41
42 // Example 3: Conditional execution - when
43 println!("3. Conditional execution - when:");
44 let add = |x: i32, y: i32| x + y;
45 let multiply = |x: i32, y: i32| x * y;
46
47 let conditional = add
48 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
49 .or_else(multiply);
50
51 println!(" When both numbers are positive, perform addition, otherwise multiplication:");
52 println!(" conditional(5, 3) = {}", conditional.apply(5, 3));
53 println!(" conditional(-5, 3) = {}", conditional.apply(-5, 3));
54 println!();
55
56 // Example 4: Complex conditional logic
57 println!("4. Complex conditional logic:");
58 let add = |x: i32, y: i32| x + y;
59 let subtract = |x: i32, y: i32| x - y;
60
61 let conditional = add
62 .when(|x: &i32, y: &i32| (*x + *y) < 100)
63 .or_else(subtract);
64
65 println!(" When sum is less than 100, perform addition, otherwise subtraction:");
66 println!(" conditional(30, 40) = {}", conditional.apply(30, 40));
67 println!(" conditional(60, 50) = {}", conditional.apply(60, 50));
68 println!();
69
70 // Example 5: String operations
71 println!("5. String operations:");
72 let concat = |x: String, y: String| format!("{}-{}", x, y);
73 let uppercase = |s: String| s.to_uppercase();
74
75 let composed = concat.and_then(uppercase);
76 let result = composed.apply("hello".to_string(), "world".to_string());
77 println!(" concat + uppercase: {}", result);
78 println!();
79
80 // Example 6: Function pointers can also be used
81 println!("6. Function pointers can also be used:");
82 fn add_fn(x: i32, y: i32) -> i32 {
83 x + y
84 }
85 fn triple(x: i32) -> i32 {
86 x * 3
87 }
88
89 let composed = add_fn.and_then(triple);
90 let result = composed.apply(4, 6);
91 println!(" (4 + 6) * 3 = {}", result);
92 println!();
93
94 // Example 7: Real application - Calculator
95 println!("7. Real application - Simple calculator:");
96 let calculate = |x: i32, y: i32| x + y;
97 let format_result = |result: i32| {
98 if result >= 0 {
99 format!("✓ Result: {}", result)
100 } else {
101 format!("✗ Negative result: {}", result)
102 }
103 };
104
105 let calculator = calculate.and_then(format_result);
106 println!(" 10 + 5 = {}", calculator.apply(10, 5));
107 println!(" -10 + 3 = {}", calculator.apply(-10, 3));
108 println!();
109
110 // Example 8: Combining multiple operations
111 println!("8. Combining multiple operations:");
112 let add = |x: i32, y: i32| x + y;
113
114 // First calculate the sum, then choose different formatting based on whether it's even
115 let sum_and_format = add.and_then(|n| {
116 if n % 2 == 0 {
117 format!("{} is even", n)
118 } else {
119 format!("{} is odd", n)
120 }
121 });
122
123 println!(" 3 + 5 = {}", sum_and_format.apply(3, 5));
124 println!(" 4 + 6 = {}", sum_and_format.apply(4, 6));
125
126 println!("\n=== Demo completed ===");
127}More examples
examples/transformers/bi_transformer_demo.rs (line 63)
17fn main() {
18 println!("=== BiTransformer Demo ===\n");
19
20 // 1. BoxBiTransformer - Single ownership
21 println!("1. BoxBiTransformer - Single ownership");
22 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
23 println!(" add.apply(20, 22) = {}", add.apply(20, 22));
24
25 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
26 println!(" multiply.apply(6, 7) = {}", multiply.apply(6, 7));
27
28 // Constant bi-transformer
29 let constant = BoxBiTransformer::constant("hello");
30 println!(" constant.apply(1, 2) = {}", constant.apply(1, 2));
31 println!();
32
33 // 2. ArcBiTransformer - Thread-safe, cloneable
34 println!("2. ArcBiTransformer - Thread-safe, cloneable");
35 let arc_add = ArcBiTransformer::new(|x: i32, y: i32| x + y);
36 let arc_add_clone = arc_add.clone();
37
38 println!(" arc_add.apply(10, 15) = {}", arc_add.apply(10, 15));
39 println!(
40 " arc_add_clone.apply(5, 8) = {}",
41 arc_add_clone.apply(5, 8)
42 );
43 println!();
44
45 // 3. RcBiTransformer - Single-threaded, cloneable
46 println!("3. RcBiTransformer - Single-threaded, cloneable");
47 let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
48 let rc_multiply_clone = rc_multiply.clone();
49
50 println!(" rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
51 println!(
52 " rc_multiply_clone.apply(5, 6) = {}",
53 rc_multiply_clone.apply(5, 6)
54 );
55 println!();
56
57 // 4. Conditional BiTransformer
58 println!("4. Conditional BiTransformer");
59 let add_if_positive = BoxBiTransformer::new(|x: i32, y: i32| x + y);
60 let multiply_otherwise = BoxBiTransformer::new(|x: i32, y: i32| x * y);
61 let conditional = add_if_positive
62 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
63 .or_else(multiply_otherwise);
64
65 println!(
66 " conditional.apply(5, 3) = {} (both positive, add)",
67 conditional.apply(5, 3)
68 );
69 println!(
70 " conditional.apply(-5, 3) = {} (not both positive, multiply)",
71 conditional.apply(-5, 3)
72 );
73 println!();
74
75 // 5. Working with different types
76 println!("5. Working with different types");
77 let format =
78 BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
79 println!(
80 " format.apply(\"Alice\", 30) = {}",
81 format.apply("Alice".to_string(), 30)
82 );
83 println!();
84
85 // 6. Closure as BiTransformer
86 println!("6. Closure as BiTransformer");
87 let subtract = |x: i32, y: i32| x - y;
88 println!(" subtract.apply(42, 10) = {}", subtract.apply(42, 10));
89 println!();
90
91 // 7. Conversion between types
92 println!("7. Conversion between types");
93 let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
94 let rc_add = box_add.into_rc();
95 println!(" Converted BoxBiTransformer to RcBiTransformer");
96 println!(" rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
97 println!();
98
99 // 8. Safe division with Option
100 println!("8. Safe division with Option");
101 let safe_divide =
102 BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
103 println!(
104 " safe_divide.apply(42, 2) = {:?}",
105 safe_divide.apply(42, 2)
106 );
107 println!(
108 " safe_divide.apply(42, 0) = {:?}",
109 safe_divide.apply(42, 0)
110 );
111 println!();
112
113 // 9. String concatenation
114 println!("9. String concatenation");
115 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
116 println!(
117 " concat.apply(\"Hello\", \"World\") = {}",
118 concat.apply("Hello".to_string(), "World".to_string())
119 );
120 println!();
121
122 println!("=== Demo Complete ===");
123}examples/transformers/bi_transformer_and_then_demo.rs (line 157)
24fn main() {
25 println!("=== BiTransformer and_then Method Demo ===\n");
26
27 // 1. BoxBiTransformer::and_then - Basic usage
28 println!("1. BoxBiTransformer::and_then - Basic usage");
29 let add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
30 let double = |x: i32| x * 2;
31 let composed = add.and_then(double);
32 println!(" (3 + 5) * 2 = {}", composed.apply(3, 5));
33 println!();
34
35 // 2. BoxBiTransformer::and_then - Chained calls
36 println!("2. BoxBiTransformer::and_then - Chained calls");
37 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
38 let add_ten = |x: i32| x + 10;
39 let to_string = |x: i32| format!("Result: {}", x);
40 let pipeline = multiply.and_then(add_ten).and_then(to_string);
41 println!(" (6 * 7) + 10 = {}", pipeline.apply(6, 7));
42 println!();
43
44 // 3. ArcBiTransformer::and_then - Shared ownership
45 println!("3. ArcBiTransformer::and_then - Shared ownership");
46 let add_arc = ArcBiTransformer::new(|x: i32, y: i32| x + y);
47 let triple = |x: i32| x * 3;
48 let composed_arc = add_arc.and_then(triple);
49
50 // Original bi-transformer is still available
51 println!(" Original: 20 + 22 = {}", add_arc.apply(20, 22));
52 println!(" Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
53 println!();
54
55 // 4. ArcBiTransformer::and_then - Cloneable
56 println!("4. ArcBiTransformer::and_then - Cloneable");
57 let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
58 let abs = |x: i32| x.abs();
59 let composed_abs = subtract.and_then(abs);
60 let cloned = composed_abs.clone();
61
62 println!(" Original: |10 - 15| = {}", composed_abs.apply(10, 15));
63 println!(" Cloned: |15 - 10| = {}", cloned.apply(15, 10));
64 println!();
65
66 // 5. RcBiTransformer::and_then - Single-threaded sharing
67 println!("5. RcBiTransformer::and_then - Single-threaded sharing");
68 let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
69 let square = |x: i32| x * x;
70 let composed_rc = divide.and_then(square);
71
72 println!(" Original: 20 / 4 = {}", divide.apply(20, 4));
73 println!(" Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
74 println!();
75
76 // 6. Type conversion example
77 println!("6. Type conversion example");
78 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
79 let to_uppercase = |s: String| s.to_uppercase();
80 let get_length = |s: String| s.len();
81
82 let uppercase_pipeline = concat.and_then(to_uppercase);
83 println!(
84 " \"hello\" + \"world\" -> uppercase: {}",
85 uppercase_pipeline.apply("hello".to_string(), "world".to_string())
86 );
87
88 let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
89 let length_pipeline = concat2.and_then(get_length);
90 println!(
91 " \"hello\" + \"world\" -> length: {}",
92 length_pipeline.apply("hello".to_string(), "world".to_string())
93 );
94 println!();
95
96 // 7. Real application: Calculator
97 println!("7. Real application: Calculator");
98 let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
99 let round = |x: f64| x.round();
100 let to_int = |x: f64| x as i32;
101
102 let calculator = calculate.and_then(round).and_then(to_int);
103 println!(
104 " 3.7 + 4.8 -> round -> integer: {}",
105 calculator.apply(3.7, 4.8)
106 );
107 println!();
108
109 // 8. Error handling example
110 println!("8. Error handling example");
111 let safe_divide = BoxBiTransformer::new(|x: i32, y: i32| -> Result<i32, String> {
112 if y == 0 {
113 Err("Division by zero is not allowed".to_string())
114 } else {
115 Ok(x / y)
116 }
117 });
118
119 let format_result = |res: Result<i32, String>| match res {
120 Ok(v) => format!("Success: {}", v),
121 Err(e) => format!("Error: {}", e),
122 };
123
124 let safe_calculator = safe_divide.and_then(format_result);
125 println!(" 10 / 2 = {}", safe_calculator.apply(10, 2));
126 println!(" 10 / 0 = {}", safe_calculator.apply(10, 0));
127 println!();
128
129 // 9. Complex data structures
130 println!("9. Complex data structures");
131 #[derive(Debug)]
132 struct Point {
133 x: i32,
134 y: i32,
135 }
136
137 let create_point = BoxBiTransformer::new(|x: i32, y: i32| Point { x, y });
138 let distance_from_origin = |p: Point| ((p.x * p.x + p.y * p.y) as f64).sqrt();
139 let format_distance = |d: f64| format!("{:.2}", d);
140
141 let point_processor = create_point
142 .and_then(distance_from_origin)
143 .and_then(format_distance);
144 println!(
145 " Distance from point(3, 4) to origin: {}",
146 point_processor.apply(3, 4)
147 );
148 println!();
149
150 // 10. Combined usage with when
151 println!("10. Combined usage with when");
152 let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
153 let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
154
155 let conditional = add_when
156 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
157 .or_else(multiply_when);
158
159 let double_result = |x: i32| x * 2;
160 let final_transformer = conditional.and_then(double_result);
161
162 println!(
163 " Add positive numbers then double: (5 + 3) * 2 = {}",
164 final_transformer.apply(5, 3)
165 );
166 println!(
167 " Multiply negative numbers then double: (-5 * 3) * 2 = {}",
168 final_transformer.apply(-5, 3)
169 );
170
171 println!("\n=== Demo completed ===");
172}Trait Implementations§
Source§impl<T, U, R> Debug for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> Debug for BoxConditionalBiTransformer<T, U, R>
Auto Trait Implementations§
impl<T, U, R> Freeze for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> !RefUnwindSafe for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> !Send for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> !Sync for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> Unpin for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> UnsafeUnpin for BoxConditionalBiTransformer<T, U, R>
impl<T, U, R> !UnwindSafe for BoxConditionalBiTransformer<T, U, 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