pub struct BoxBiTransformerOnce<T, U, R> { /* private fields */ }Expand description
BoxBiTransformerOnce - consuming bi-transformer wrapper based on
Box<dyn FnOnce>
A bi-transformer wrapper that provides single ownership with one-time use semantics. Consumes self and both input values.
§Features
- Based on:
Box<dyn FnOnce(T, U) -> R> - Ownership: Single ownership, cannot be cloned
- Reusability: Can only be called once (consumes self and inputs)
- Thread Safety: Not thread-safe (no
Send + Syncrequirement)
§Author
Hu Haixing
Implementations§
Source§impl<T, U, R> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
impl<T, U, R> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: 'static,
Sourcepub fn new<F>(f: F) -> Selfwhere
F: FnOnce(T, U) -> R + 'static,
pub fn new<F>(f: F) -> Selfwhere
F: FnOnce(T, U) -> R + 'static,
Creates a new BoxBiTransformerOnce
§Parameters
f- The closure or function to wrap
§Examples
use prism3_function::{BoxBiTransformerOnce, BiTransformerOnce};
let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
assert_eq!(add.apply_once(20, 22), 42);Examples found in repository?
15fn main() {
16 println!("=== BoxBiTransformerOnce and_then Method Example ===\n");
17
18 // Example 1: Basic and_then usage
19 println!("Example 1: Basic and_then usage");
20 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
21 let double = |x: i32| x * 2;
22 let composed = add.and_then(double);
23 let result = composed.apply_once(3, 5);
24 println!(" (3 + 5) * 2 = {}", result);
25 assert_eq!(result, 16);
26 println!();
27
28 // Example 2: Type conversion
29 println!("Example 2: Type conversion");
30 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
31 let to_string = |x: i32| x.to_string();
32 let composed2 = add2.and_then(to_string);
33 let result2 = composed2.apply_once(20, 22);
34 println!(" (20 + 22).to_string() = \"{}\"", result2);
35 assert_eq!(result2, "42");
36 println!();
37
38 // Example 3: Multi-level chained composition
39 println!("Example 3: Multi-level chained composition");
40 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
41 let double3 = |x: i32| x * 2;
42 let to_string3 = |x: i32| format!("Result: {}", x);
43 let composed3 = add3.and_then(double3).and_then(to_string3);
44 let result3 = composed3.apply_once(3, 5);
45 println!(" (3 + 5) * 2 -> \"{}\"", result3);
46 assert_eq!(result3, "Result: 16");
47 println!();
48
49 // Example 4: String operations
50 println!("Example 4: String operations");
51 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
52 let uppercase = |s: String| s.to_uppercase();
53 let composed4 = concat.and_then(uppercase);
54 let result4 = composed4.apply_once("hello".to_string(), "world".to_string());
55 println!(" \"hello\" + \"world\" -> uppercase = \"{}\"", result4);
56 assert_eq!(result4, "HELLO WORLD");
57 println!();
58
59 // Example 5: Mathematical calculation chain
60 println!("Example 5: Mathematical calculation chain");
61 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
62 let to_float = |x: i32| x as f64 / 2.0;
63 let composed5 = multiply.and_then(to_float);
64 let result5 = composed5.apply_once(6, 7);
65 println!(" (6 * 7) / 2.0 = {}", result5);
66 assert!((result5 - 21.0).abs() < 1e-10);
67 println!();
68
69 // Example 6: Complex business logic
70 println!("Example 6: Complex business logic");
71 let calculate_total =
72 BoxBiTransformerOnce::new(|price: f64, quantity: i32| price * quantity as f64);
73 let apply_discount = |total: f64| {
74 if total > 100.0 {
75 total * 0.9 // 10% discount
76 } else {
77 total
78 }
79 };
80 let format_price = |total: f64| format!("${:.2}", total);
81 let composed6 = calculate_total
82 .and_then(apply_discount)
83 .and_then(format_price);
84 let result6 = composed6.apply_once(15.5, 8);
85 println!(" Price: $15.5, Quantity: 8");
86 println!(" Total price (with discount): {}", result6);
87 assert_eq!(result6, "$111.60");
88 println!();
89
90 println!("=== All examples executed successfully! ===");
91}More examples
13fn main() {
14 println!("=== BiTransformerOnce Examples ===\n");
15
16 // Example 1: Basic usage with closure
17 println!("1. Basic usage with closure:");
18 let add = |x: i32, y: i32| x + y;
19 let result = add.apply_once(20, 22);
20 println!(" 20 + 22 = {}", result);
21
22 // Example 2: BoxBiTransformerOnce with new
23 println!("\n2. BoxBiTransformerOnce with new:");
24 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
25 println!(" 6 * 7 = {}", multiply.apply_once(6, 7));
26
27 // Example 3: Constant transformer
28 println!("\n3. Constant transformer:");
29 let constant = BoxBiTransformerOnce::constant("hello");
30 println!(" constant(123, 456) = {}", constant.apply_once(123, 456));
31
32 // Example 4: Consuming owned values
33 println!("\n4. Consuming owned values:");
34 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
35 let s1 = String::from("hello");
36 let s2 = String::from("world");
37 let result = concat.apply_once(s1, s2);
38 println!(" concat('hello', 'world') = {}", result);
39
40 // Example 5: Conditional transformation with when/or_else
41 println!("\n5. Conditional transformation (positive numbers):");
42 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
43 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
44 let conditional = add
45 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
46 .or_else(multiply);
47 println!(
48 " conditional(5, 3) = {} (add)",
49 conditional.apply_once(5, 3)
50 );
51
52 println!("\n6. Conditional transformation (negative numbers):");
53 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
54 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
55 let conditional2 = add2
56 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
57 .or_else(multiply2);
58 println!(
59 " conditional(-5, 3) = {} (multiply)",
60 conditional2.apply_once(-5, 3)
61 );
62
63 // Example 7: Conditional with closure in or_else
64 println!("\n7. Conditional with closure in or_else:");
65 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
66 let conditional3 = add3
67 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68 .or_else(|x: i32, y: i32| x * y);
69 println!(" conditional(4, 6) = {}", conditional3.apply_once(4, 6));
70
71 // Example 8: Merging vectors
72 println!("\n8. Merging vectors:");
73 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
74 x.extend(y);
75 x
76 });
77 let v1 = vec![1, 2, 3];
78 let v2 = vec![4, 5, 6];
79 let result = merge.apply_once(v1, v2);
80 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
81
82 // Example 9: Complex transformation with calculation
83 println!("\n9. Complex transformation with calculation:");
84 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
85 let sum = x + y;
86 let product = x * y;
87 (sum, product)
88 });
89 let (sum, product) = calculate.apply_once(5, 3);
90 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
91
92 // Example 10: String manipulation
93 println!("\n10. String manipulation:");
94 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
95 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
96 });
97 println!(
98 " process('Hello', 'WORLD') = {}",
99 process.apply_once("Hello".to_string(), "WORLD".to_string())
100 );
101
102 // Example 11: Converting to function
103 println!("\n11. Converting to function:");
104 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105 let f = add4.into_fn_once();
106 println!(" f(10, 20) = {}", f(10, 20));
107
108 // Example 12: Converting to box (zero-cost)
109 println!("\n12. Converting to box (zero-cost):");
110 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
111 let boxed = add5.into_box_once();
112 println!(" boxed(15, 25) = {}", boxed.apply_once(15, 25));
113
114 println!("\n=== All examples completed successfully! ===");
115}Sourcepub fn and_then<S, F>(self, after: F) -> BoxBiTransformerOnce<T, U, S>where
S: 'static,
F: TransformerOnce<R, S> + 'static,
pub fn and_then<S, F>(self, after: F) -> BoxBiTransformerOnce<T, U, S>where
S: 'static,
F: TransformerOnce<R, S> + 'static,
Chain composition - applies self first, then after
Creates a new bi-transformer that applies this bi-transformer first,
then applies the after transformer to the result. Consumes self and
returns a new BoxBiTransformerOnce.
§Type Parameters
S- The output type of the after transformerF- The type of the after transformer (must implement TransformerOnce<R, S>)
§Parameters
after- The transformer to apply after self. Note: This parameter is passed by value and will transfer ownership. SinceBoxBiTransformerOncecannot be cloned, the parameter will be consumed. Can be:- A closure:
|x: R| -> S - A function pointer:
fn(R) -> S - A
BoxTransformerOnce<R, S> - Any type implementing
TransformerOnce<R, S>
- A closure:
§Returns
A new BoxBiTransformerOnce<T, U, S> representing the composition
§Examples
use prism3_function::{BiTransformerOnce, BoxBiTransformerOnce};
let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let double = |x: i32| x * 2;
// Both add and double are moved and consumed
let composed = add.and_then(double);
assert_eq!(composed.apply_once(3, 5), 16); // (3 + 5) * 2
// add.apply_once(1, 2); // Would not compile - moved
// double(10); // Would not compile - movedExamples found in repository?
15fn main() {
16 println!("=== BoxBiTransformerOnce and_then Method Example ===\n");
17
18 // Example 1: Basic and_then usage
19 println!("Example 1: Basic and_then usage");
20 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
21 let double = |x: i32| x * 2;
22 let composed = add.and_then(double);
23 let result = composed.apply_once(3, 5);
24 println!(" (3 + 5) * 2 = {}", result);
25 assert_eq!(result, 16);
26 println!();
27
28 // Example 2: Type conversion
29 println!("Example 2: Type conversion");
30 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
31 let to_string = |x: i32| x.to_string();
32 let composed2 = add2.and_then(to_string);
33 let result2 = composed2.apply_once(20, 22);
34 println!(" (20 + 22).to_string() = \"{}\"", result2);
35 assert_eq!(result2, "42");
36 println!();
37
38 // Example 3: Multi-level chained composition
39 println!("Example 3: Multi-level chained composition");
40 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
41 let double3 = |x: i32| x * 2;
42 let to_string3 = |x: i32| format!("Result: {}", x);
43 let composed3 = add3.and_then(double3).and_then(to_string3);
44 let result3 = composed3.apply_once(3, 5);
45 println!(" (3 + 5) * 2 -> \"{}\"", result3);
46 assert_eq!(result3, "Result: 16");
47 println!();
48
49 // Example 4: String operations
50 println!("Example 4: String operations");
51 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
52 let uppercase = |s: String| s.to_uppercase();
53 let composed4 = concat.and_then(uppercase);
54 let result4 = composed4.apply_once("hello".to_string(), "world".to_string());
55 println!(" \"hello\" + \"world\" -> uppercase = \"{}\"", result4);
56 assert_eq!(result4, "HELLO WORLD");
57 println!();
58
59 // Example 5: Mathematical calculation chain
60 println!("Example 5: Mathematical calculation chain");
61 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
62 let to_float = |x: i32| x as f64 / 2.0;
63 let composed5 = multiply.and_then(to_float);
64 let result5 = composed5.apply_once(6, 7);
65 println!(" (6 * 7) / 2.0 = {}", result5);
66 assert!((result5 - 21.0).abs() < 1e-10);
67 println!();
68
69 // Example 6: Complex business logic
70 println!("Example 6: Complex business logic");
71 let calculate_total =
72 BoxBiTransformerOnce::new(|price: f64, quantity: i32| price * quantity as f64);
73 let apply_discount = |total: f64| {
74 if total > 100.0 {
75 total * 0.9 // 10% discount
76 } else {
77 total
78 }
79 };
80 let format_price = |total: f64| format!("${:.2}", total);
81 let composed6 = calculate_total
82 .and_then(apply_discount)
83 .and_then(format_price);
84 let result6 = composed6.apply_once(15.5, 8);
85 println!(" Price: $15.5, Quantity: 8");
86 println!(" Total price (with discount): {}", result6);
87 assert_eq!(result6, "$111.60");
88 println!();
89
90 println!("=== All examples executed successfully! ===");
91}Sourcepub fn when<P>(self, predicate: P) -> BoxConditionalBiTransformerOnce<T, U, R>where
P: BiPredicate<T, U> + 'static,
pub fn when<P>(self, predicate: P) -> BoxConditionalBiTransformerOnce<T, U, R>where
P: BiPredicate<T, U> + 'static,
Creates a conditional bi-transformer
Returns a bi-transformer that only executes when a bi-predicate is
satisfied. You must call or_else() to provide an alternative
bi-transformer.
§Parameters
predicate- The condition to check. Note: This parameter is passed by value and will transfer ownership. If you need to preserve the original bi-predicate, clone it first (if it implementsClone). Can be:- A closure:
|x: &T, y: &U| -> bool - A function pointer:
fn(&T, &U) -> bool - A
BoxBiPredicate<T, U> - An
RcBiPredicate<T, U> - An
ArcBiPredicate<T, U> - Any type implementing
BiPredicate<T, U>
- A closure:
§Returns
Returns BoxConditionalBiTransformerOnce<T, U, R>
§Examples
§Basic usage with or_else
use prism3_function::{BiTransformerOnce, BoxBiTransformerOnce};
let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
let conditional = add.when(|x: &i32, y: &i32| *x > 0 && *y > 0)
.or_else(multiply);
assert_eq!(conditional.apply_once(5, 3), 8);
let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
let conditional2 = add2.when(|x: &i32, y: &i32| *x > 0 && *y > 0)
.or_else(multiply2);
assert_eq!(conditional2.apply_once(-5, 3), -15);§Preserving bi-predicate with clone
use prism3_function::{BiTransformerOnce, BoxBiTransformerOnce, RcBiPredicate};
let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
let both_positive = RcBiPredicate::new(|x: &i32, y: &i32|
*x > 0 && *y > 0);
// Clone to preserve original bi-predicate
let conditional = add.when(both_positive.clone())
.or_else(BoxBiTransformerOnce::new(|x, y| x * y));
assert_eq!(conditional.apply_once(5, 3), 8);
// Original bi-predicate still usable
assert!(both_positive.test(&5, &3));Examples found in repository?
13fn main() {
14 println!("=== BiTransformerOnce Examples ===\n");
15
16 // Example 1: Basic usage with closure
17 println!("1. Basic usage with closure:");
18 let add = |x: i32, y: i32| x + y;
19 let result = add.apply_once(20, 22);
20 println!(" 20 + 22 = {}", result);
21
22 // Example 2: BoxBiTransformerOnce with new
23 println!("\n2. BoxBiTransformerOnce with new:");
24 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
25 println!(" 6 * 7 = {}", multiply.apply_once(6, 7));
26
27 // Example 3: Constant transformer
28 println!("\n3. Constant transformer:");
29 let constant = BoxBiTransformerOnce::constant("hello");
30 println!(" constant(123, 456) = {}", constant.apply_once(123, 456));
31
32 // Example 4: Consuming owned values
33 println!("\n4. Consuming owned values:");
34 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
35 let s1 = String::from("hello");
36 let s2 = String::from("world");
37 let result = concat.apply_once(s1, s2);
38 println!(" concat('hello', 'world') = {}", result);
39
40 // Example 5: Conditional transformation with when/or_else
41 println!("\n5. Conditional transformation (positive numbers):");
42 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
43 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
44 let conditional = add
45 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
46 .or_else(multiply);
47 println!(
48 " conditional(5, 3) = {} (add)",
49 conditional.apply_once(5, 3)
50 );
51
52 println!("\n6. Conditional transformation (negative numbers):");
53 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
54 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
55 let conditional2 = add2
56 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
57 .or_else(multiply2);
58 println!(
59 " conditional(-5, 3) = {} (multiply)",
60 conditional2.apply_once(-5, 3)
61 );
62
63 // Example 7: Conditional with closure in or_else
64 println!("\n7. Conditional with closure in or_else:");
65 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
66 let conditional3 = add3
67 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68 .or_else(|x: i32, y: i32| x * y);
69 println!(" conditional(4, 6) = {}", conditional3.apply_once(4, 6));
70
71 // Example 8: Merging vectors
72 println!("\n8. Merging vectors:");
73 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
74 x.extend(y);
75 x
76 });
77 let v1 = vec![1, 2, 3];
78 let v2 = vec![4, 5, 6];
79 let result = merge.apply_once(v1, v2);
80 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
81
82 // Example 9: Complex transformation with calculation
83 println!("\n9. Complex transformation with calculation:");
84 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
85 let sum = x + y;
86 let product = x * y;
87 (sum, product)
88 });
89 let (sum, product) = calculate.apply_once(5, 3);
90 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
91
92 // Example 10: String manipulation
93 println!("\n10. String manipulation:");
94 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
95 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
96 });
97 println!(
98 " process('Hello', 'WORLD') = {}",
99 process.apply_once("Hello".to_string(), "WORLD".to_string())
100 );
101
102 // Example 11: Converting to function
103 println!("\n11. Converting to function:");
104 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105 let f = add4.into_fn_once();
106 println!(" f(10, 20) = {}", f(10, 20));
107
108 // Example 12: Converting to box (zero-cost)
109 println!("\n12. Converting to box (zero-cost):");
110 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
111 let boxed = add5.into_box_once();
112 println!(" boxed(15, 25) = {}", boxed.apply_once(15, 25));
113
114 println!("\n=== All examples completed successfully! ===");
115}Source§impl<T, U, R> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
impl<T, U, R> BoxBiTransformerOnce<T, U, R>where
T: 'static,
U: 'static,
R: Clone + 'static,
Sourcepub fn constant(value: R) -> BoxBiTransformerOnce<T, U, R>
pub fn constant(value: R) -> BoxBiTransformerOnce<T, U, R>
Creates a constant bi-transformer
§Examples
use prism3_function::{BoxBiTransformerOnce, BiTransformerOnce};
let constant = BoxBiTransformerOnce::constant("hello");
assert_eq!(constant.apply_once(123, 456), "hello");Examples found in repository?
13fn main() {
14 println!("=== BiTransformerOnce Examples ===\n");
15
16 // Example 1: Basic usage with closure
17 println!("1. Basic usage with closure:");
18 let add = |x: i32, y: i32| x + y;
19 let result = add.apply_once(20, 22);
20 println!(" 20 + 22 = {}", result);
21
22 // Example 2: BoxBiTransformerOnce with new
23 println!("\n2. BoxBiTransformerOnce with new:");
24 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
25 println!(" 6 * 7 = {}", multiply.apply_once(6, 7));
26
27 // Example 3: Constant transformer
28 println!("\n3. Constant transformer:");
29 let constant = BoxBiTransformerOnce::constant("hello");
30 println!(" constant(123, 456) = {}", constant.apply_once(123, 456));
31
32 // Example 4: Consuming owned values
33 println!("\n4. Consuming owned values:");
34 let concat = BoxBiTransformerOnce::new(|x: String, y: String| format!("{} {}", x, y));
35 let s1 = String::from("hello");
36 let s2 = String::from("world");
37 let result = concat.apply_once(s1, s2);
38 println!(" concat('hello', 'world') = {}", result);
39
40 // Example 5: Conditional transformation with when/or_else
41 println!("\n5. Conditional transformation (positive numbers):");
42 let add = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
43 let multiply = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
44 let conditional = add
45 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
46 .or_else(multiply);
47 println!(
48 " conditional(5, 3) = {} (add)",
49 conditional.apply_once(5, 3)
50 );
51
52 println!("\n6. Conditional transformation (negative numbers):");
53 let add2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
54 let multiply2 = BoxBiTransformerOnce::new(|x: i32, y: i32| x * y);
55 let conditional2 = add2
56 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
57 .or_else(multiply2);
58 println!(
59 " conditional(-5, 3) = {} (multiply)",
60 conditional2.apply_once(-5, 3)
61 );
62
63 // Example 7: Conditional with closure in or_else
64 println!("\n7. Conditional with closure in or_else:");
65 let add3 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
66 let conditional3 = add3
67 .when(|x: &i32, y: &i32| *x > 0 && *y > 0)
68 .or_else(|x: i32, y: i32| x * y);
69 println!(" conditional(4, 6) = {}", conditional3.apply_once(4, 6));
70
71 // Example 8: Merging vectors
72 println!("\n8. Merging vectors:");
73 let merge = BoxBiTransformerOnce::new(|mut x: Vec<i32>, y: Vec<i32>| {
74 x.extend(y);
75 x
76 });
77 let v1 = vec![1, 2, 3];
78 let v2 = vec![4, 5, 6];
79 let result = merge.apply_once(v1, v2);
80 println!(" merge([1, 2, 3], [4, 5, 6]) = {:?}", result);
81
82 // Example 9: Complex transformation with calculation
83 println!("\n9. Complex transformation with calculation:");
84 let calculate = BoxBiTransformerOnce::new(|x: i32, y: i32| {
85 let sum = x + y;
86 let product = x * y;
87 (sum, product)
88 });
89 let (sum, product) = calculate.apply_once(5, 3);
90 println!(" calculate(5, 3) = (sum: {}, product: {})", sum, product);
91
92 // Example 10: String manipulation
93 println!("\n10. String manipulation:");
94 let process = BoxBiTransformerOnce::new(|x: String, y: String| {
95 format!("{} {} {}", x.to_uppercase(), "and", y.to_lowercase())
96 });
97 println!(
98 " process('Hello', 'WORLD') = {}",
99 process.apply_once("Hello".to_string(), "WORLD".to_string())
100 );
101
102 // Example 11: Converting to function
103 println!("\n11. Converting to function:");
104 let add4 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
105 let f = add4.into_fn_once();
106 println!(" f(10, 20) = {}", f(10, 20));
107
108 // Example 12: Converting to box (zero-cost)
109 println!("\n12. Converting to box (zero-cost):");
110 let add5 = BoxBiTransformerOnce::new(|x: i32, y: i32| x + y);
111 let boxed = add5.into_box_once();
112 println!(" boxed(15, 25) = {}", boxed.apply_once(15, 25));
113
114 println!("\n=== All examples completed successfully! ===");
115}