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