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