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.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>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.apply(5, 3), 8); // Condition satisfied, execute add
assert_eq!(conditional.apply(-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.apply(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.apply(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.apply(5, 3));
50 println!(" conditional(-5, 3) = {}", conditional.apply(-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.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/bi_transformer_demo.rs (line 58)
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.apply(20, 22) = {}", add.apply(20, 22));
19
20 let multiply = BoxBiTransformer::new(|x: i32, y: i32| x * y);
21 println!(" multiply.apply(6, 7) = {}", multiply.apply(6, 7));
22
23 // Constant bi-transformer
24 let constant = BoxBiTransformer::constant("hello");
25 println!(" constant.apply(1, 2) = {}", constant.apply(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!(" arc_add.apply(10, 15) = {}", arc_add.apply(10, 15));
34 println!(
35 " arc_add_clone.apply(5, 8) = {}",
36 arc_add_clone.apply(5, 8)
37 );
38 println!();
39
40 // 3. RcBiTransformer - Single-threaded, cloneable
41 println!("3. RcBiTransformer - Single-threaded, cloneable");
42 let rc_multiply = RcBiTransformer::new(|x: i32, y: i32| x * y);
43 let rc_multiply_clone = rc_multiply.clone();
44
45 println!(" rc_multiply.apply(3, 4) = {}", rc_multiply.apply(3, 4));
46 println!(
47 " rc_multiply_clone.apply(5, 6) = {}",
48 rc_multiply_clone.apply(5, 6)
49 );
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 =
73 BoxBiTransformer::new(|name: String, age: i32| format!("{} is {} years old", name, age));
74 println!(
75 " format.apply(\"Alice\", 30) = {}",
76 format.apply("Alice".to_string(), 30)
77 );
78 println!();
79
80 // 6. Closure as BiTransformer
81 println!("6. Closure as BiTransformer");
82 let subtract = |x: i32, y: i32| x - y;
83 println!(" subtract.apply(42, 10) = {}", subtract.apply(42, 10));
84 println!();
85
86 // 7. Conversion between types
87 println!("7. Conversion between types");
88 let box_add = BoxBiTransformer::new(|x: i32, y: i32| x + y);
89 let rc_add = box_add.into_rc();
90 println!(" Converted BoxBiTransformer to RcBiTransformer");
91 println!(" rc_add.apply(7, 8) = {}", rc_add.apply(7, 8));
92 println!();
93
94 // 8. Safe division with Option
95 println!("8. Safe division with Option");
96 let safe_divide =
97 BoxBiTransformer::new(|x: i32, y: i32| if y == 0 { None } else { Some(x / y) });
98 println!(
99 " safe_divide.apply(42, 2) = {:?}",
100 safe_divide.apply(42, 2)
101 );
102 println!(
103 " safe_divide.apply(42, 0) = {:?}",
104 safe_divide.apply(42, 0)
105 );
106 println!();
107
108 // 9. String concatenation
109 println!("9. String concatenation");
110 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{}{}", s1, s2));
111 println!(
112 " concat.apply(\"Hello\", \"World\") = {}",
113 concat.apply("Hello".to_string(), "World".to_string())
114 );
115 println!();
116
117 println!("=== Demo Complete ===");
118}examples/bi_transformer_and_then_demo.rs (line 152)
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.apply(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.apply(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.apply(20, 22));
47 println!(" Composed: (5 + 3) * 3 = {}", composed_arc.apply(5, 3));
48 println!();
49
50 // 4. ArcBiTransformer::and_then - Cloneable
51 println!("4. ArcBiTransformer::and_then - Cloneable");
52 let subtract = ArcBiTransformer::new(|x: i32, y: i32| x - y);
53 let abs = |x: i32| x.abs();
54 let composed_abs = subtract.and_then(abs);
55 let cloned = composed_abs.clone();
56
57 println!(" Original: |10 - 15| = {}", composed_abs.apply(10, 15));
58 println!(" Cloned: |15 - 10| = {}", cloned.apply(15, 10));
59 println!();
60
61 // 5. RcBiTransformer::and_then - Single-threaded sharing
62 println!("5. RcBiTransformer::and_then - Single-threaded sharing");
63 let divide = RcBiTransformer::new(|x: i32, y: i32| x / y);
64 let square = |x: i32| x * x;
65 let composed_rc = divide.and_then(square);
66
67 println!(" Original: 20 / 4 = {}", divide.apply(20, 4));
68 println!(" Composed: (20 / 4)² = {}", composed_rc.apply(20, 4));
69 println!();
70
71 // 6. Type conversion example
72 println!("6. Type conversion example");
73 let concat = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
74 let to_uppercase = |s: String| s.to_uppercase();
75 let get_length = |s: String| s.len();
76
77 let uppercase_pipeline = concat.and_then(to_uppercase);
78 println!(
79 " \"hello\" + \"world\" -> uppercase: {}",
80 uppercase_pipeline.apply("hello".to_string(), "world".to_string())
81 );
82
83 let concat2 = BoxBiTransformer::new(|s1: String, s2: String| format!("{} {}", s1, s2));
84 let length_pipeline = concat2.and_then(get_length);
85 println!(
86 " \"hello\" + \"world\" -> length: {}",
87 length_pipeline.apply("hello".to_string(), "world".to_string())
88 );
89 println!();
90
91 // 7. Real application: Calculator
92 println!("7. Real application: Calculator");
93 let calculate = BoxBiTransformer::new(|a: f64, b: f64| a + b);
94 let round = |x: f64| x.round();
95 let to_int = |x: f64| x as i32;
96
97 let calculator = calculate.and_then(round).and_then(to_int);
98 println!(
99 " 3.7 + 4.8 -> round -> integer: {}",
100 calculator.apply(3.7, 4.8)
101 );
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
137 .and_then(distance_from_origin)
138 .and_then(format_distance);
139 println!(
140 " Distance from point(3, 4) to origin: {}",
141 point_processor.apply(3, 4)
142 );
143 println!();
144
145 // 10. Combined usage with when
146 println!("10. Combined usage with when");
147 let add_when = BoxBiTransformer::new(|x: i32, y: i32| x + y);
148 let multiply_when = BoxBiTransformer::new(|x: i32, y: i32| x * y);
149
150 let conditional = add_when
151 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
152 .or_else(multiply_when);
153
154 let double_result = |x: i32| x * 2;
155 let final_transformer = conditional.and_then(double_result);
156
157 println!(
158 " Add positive numbers then double: (5 + 3) * 2 = {}",
159 final_transformer.apply(5, 3)
160 );
161 println!(
162 " Multiply negative numbers then double: (-5 * 3) * 2 = {}",
163 final_transformer.apply(-5, 3)
164 );
165
166 println!("\n=== Demo completed ===");
167}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