Skip to main content

BoxBiTransformer

Struct BoxBiTransformer 

Source
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 + Sync requirement)

Implementations§

Source§

impl<T, U, R> BoxBiTransformer<T, U, R>

Source

pub fn new<F>(f: F) -> Self
where 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?
examples/transformers/bi_transformer_demo.rs (line 23)
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
Hide additional examples
examples/transformers/bi_transformer_and_then_demo.rs (line 27)
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

pub fn new_with_name<F>(name: &str, f: F) -> Self
where 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.

Source

pub fn new_with_optional_name<F>(f: F, name: Option<String>) -> Self
where 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.

Source

pub fn name(&self) -> Option<&str>

Gets the name of this bi-transformer.

§Returns

Returns Some(&str) if a name was set, None otherwise.

Source

pub fn set_name(&mut self, name: &str)

Sets the name of this bi-transformer.

§Parameters
  • name - The name to set for this bi-transformer
Source

pub fn clear_name(&mut self)

Clears the name of this bi-transformer.

Source

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?
examples/transformers/bi_transformer_demo.rs (line 63)
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
Hide additional examples
examples/transformers/bi_transformer_and_then_demo.rs (line 154)
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

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) = 6
Examples found in repository?
examples/transformers/bi_transformer_and_then_demo.rs (line 29)
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>

Source

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?
examples/transformers/bi_transformer_demo.rs (line 30)
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}

Trait Implementations§

Source§

impl<T, U, R> BiTransformer<T, U, R> for BoxBiTransformer<T, U, R>

Source§

fn apply(&self, first: T, second: U) -> R

Transforms two input values to produce an output value Read more
Source§

fn into_box(self) -> BoxBiTransformer<T, U, R>

Converts to BoxBiTransformer Read more
Source§

fn into_rc(self) -> RcBiTransformer<T, U, R>
where Self: 'static,

Converts to RcBiTransformer Read more
Source§

fn into_fn(self) -> impl Fn(T, U) -> R

Converts bi-transformer to a closure Read more
Source§

fn into_once(self) -> BoxBiTransformerOnce<T, U, R>
where Self: 'static,

Convert to BiTransformerOnce Read more
Source§

fn into_arc(self) -> ArcBiTransformer<T, U, R>
where Self: Sized + Send + Sync + 'static,

Converts to ArcBiTransformer Read more
Source§

impl<T, U, R> Debug for BoxBiTransformer<T, U, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, U, R> Display for BoxBiTransformer<T, U, R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, U, R> Freeze for BoxBiTransformer<T, U, R>

§

impl<T, U, R> !RefUnwindSafe for BoxBiTransformer<T, U, R>

§

impl<T, U, R> !Send for BoxBiTransformer<T, U, R>

§

impl<T, U, R> !Sync for BoxBiTransformer<T, U, R>

§

impl<T, U, R> Unpin for BoxBiTransformer<T, U, R>

§

impl<T, U, R> UnsafeUnpin for BoxBiTransformer<T, U, R>

§

impl<T, U, R> !UnwindSafe for BoxBiTransformer<T, U, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<F, T> BinaryOperator<T> for F
where F: BiTransformer<T, T, T>,