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