pub struct BoxBiTransformer<T, U, R> { /* private fields */ }Expand description
BoxBiTransformer - bi-transformer wrapper based on Box<dyn Fn>
A bi-transformer wrapper that provides single ownership with reusable transformation. The bi-transformer consumes both inputs and can be called multiple times.
§Features
- Based on:
Box<dyn Fn(T, U) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can be called multiple times (each call consumes its inputs)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
Implementations§
Source§impl<T, U, R> BoxBiTransformer<T, U, R>
impl<T, U, R> BoxBiTransformer<T, U, R>
Sourcepub fn new<F>(f: F) -> Selfwhere
F: Fn(T, U) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: Fn(T, U) -> R + 'static,
Creates a new bi-transformer.
Wraps the provided closure in the appropriate smart pointer type for this bi-transformer implementation.
Examples found in repository?
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}More examples
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}Sourcepub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn(T, U) -> R + 'static,
pub fn new_with_name<F>(name: &str, f: F) -> Selfwhere
F: Fn(T, U) -> R + 'static,
Creates a new named bi-transformer.
Wraps the provided closure and assigns it a name, which is useful for debugging and logging purposes.
Sourcepub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn(T, U) -> R + 'static,
pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Selfwhere
F: Fn(T, U) -> R + 'static,
Creates a new named bi-transformer with an optional name.
Wraps the provided closure and assigns it an optional name.
Sourcepub fn name(&self) -> Option<&str>
pub fn name(&self) -> Option<&str>
Gets the name of this bi-transformer.
§Returns
Returns Some(&str) if a name was set, None otherwise.
Sourcepub fn clear_name(&mut self)
pub fn clear_name(&mut self)
Clears the name of this bi-transformer.
Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalBiTransformer<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
P: BiPredicate<T, U> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalBiTransformer<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
P: BiPredicate<T, U> + 'static,
Creates a conditional two-parameter transformer that executes based on bi-predicate result.
§Parameters
predicate- The bi-predicate to determine whether to execute the transformation operation
§Returns
Returns a conditional two-parameter transformer that only executes
when the predicate returns true.
§Examples
use qubit_function::transformers::*;
let bi_transformer = BoxBiTransformer::new({
|key: String, value: i32| format!("{}: {}", key, value)
});
let conditional = bi_transformer
.when(|key: &String, value: &i32| *value > 0)
.or_else(|key: String, _value: i32| key);
assert_eq!(conditional.apply("test".to_string(), 5), "test: 5".to_string()); // transformed
assert_eq!(conditional.apply("test".to_string(), -1), "test".to_string()); // identity (key unchanged)Examples found in repository?
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}More examples
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}Sourcepub fn and_then<S, F>(self, after: F) -> BoxBiTransformer<T, U, S>where
T: 'static,
U: 'static,
R: 'static,
S: 'static,
F: Transformer<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxBiTransformer<T, U, S>where
T: 'static,
U: 'static,
R: 'static,
S: 'static,
F: Transformer<R, S> + 'static,
Chains execution with another two-parameter transformer, executing the current transformer first, then the subsequent transformer.
§Parameters
after- The subsequent two-parameter transformer to execute after the current transformer completes
§Returns
Returns a new two-parameter transformer that executes the current transformer and the subsequent transformer in sequence.
§Examples
use qubit_function::transformers::*;
let bi_transformer1 = BoxBiTransformer::new({
|key: String, value: i32| (key, value + 1)
});
let bi_transformer2 = BoxTransformer::new({
|value: (String, i32)| format!("{}: {}", value.0, value.1)
});
let chained = bi_transformer1.and_then(bi_transformer2);
let result = chained.apply("test".to_string(), 5);
assert_eq!(result, "test: 6"); // (value + 1) = 6Examples found in repository?
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}Source§impl<T, U, R> BoxBiTransformer<T, U, R>
impl<T, U, R> BoxBiTransformer<T, U, R>
Sourcepub fn constant(value: R) -> BoxBiTransformer<T, U, R>where
R: Clone + 'static,
pub fn constant(value: R) -> BoxBiTransformer<T, U, R>where
R: Clone + 'static,
Creates a constant bi-transformer
§Examples
/// rust /// use qubit_function::{BoxBiTransformer, BiTransformer}; /// /// let constant = BoxBiTransformer::constant("hello"); /// assert_eq!(constant.apply(123, 456), "hello"); ///
Examples found in repository?
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}