transformer_demo/
transformer_demo.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9
10use prism3_function::{ArcTransformer, BoxTransformer, RcTransformer, Transformer};
11use std::collections::HashMap;
12use std::thread;
13
14fn main() {
15    println!("=== Transformer Demo - Type Transformation (consumes T) ===\n");
16
17    // ====================================================================
18    // Part 1: BoxTransformer - Single ownership, reusable
19    // ====================================================================
20    println!("--- BoxTransformer ---");
21    let double = BoxTransformer::new(|x: i32| x * 2);
22    println!("double.apply(21) = {}", double.apply(21));
23    println!("double.apply(42) = {}", double.apply(42));
24
25    // Identity and constant
26    let identity = BoxTransformer::<i32, i32>::identity();
27    println!("identity.apply(42) = {}", identity.apply(42));
28
29    let constant = BoxTransformer::constant("hello");
30    println!("constant.apply(123) = {}", constant.apply(123));
31    println!();
32
33    // ====================================================================
34    // Part 2: ArcTransformer - Thread-safe, cloneable
35    // ====================================================================
36    println!("--- ArcTransformer ---");
37    let arc_double = ArcTransformer::new(|x: i32| x * 2);
38    let arc_cloned = arc_double.clone();
39
40    println!("arc_double.apply(21) = {}", arc_double.apply(21));
41    println!("arc_cloned.apply(42) = {}", arc_cloned.apply(42));
42
43    // Multi-threaded usage
44    let for_thread = arc_double.clone();
45    let handle = thread::spawn(move || for_thread.apply(100));
46    println!(
47        "In main thread: arc_double.apply(50) = {}",
48        arc_double.apply(50)
49    );
50    println!("In child thread: result = {}", handle.join().unwrap());
51    println!();
52
53    // ====================================================================
54    // Part 3: RcTransformer - Single-threaded, cloneable
55    // ====================================================================
56    println!("--- RcTransformer ---");
57    let rc_double = RcTransformer::new(|x: i32| x * 2);
58    let rc_cloned = rc_double.clone();
59
60    println!("rc_double.apply(21) = {}", rc_double.apply(21));
61    println!("rc_cloned.apply(42) = {}", rc_cloned.apply(42));
62    println!();
63
64    // ====================================================================
65    // Part 4: Practical Examples
66    // ====================================================================
67    println!("=== Practical Examples ===\n");
68
69    // Example 1: String transformation
70    println!("--- String Transformation ---");
71    let to_upper = BoxTransformer::new(|s: String| s.to_uppercase());
72    println!(
73        "to_upper.apply('hello') = {}",
74        to_upper.apply("hello".to_string())
75    );
76    println!(
77        "to_upper.apply('world') = {}",
78        to_upper.apply("world".to_string())
79    );
80    println!();
81
82    // Example 2: Type conversion pipeline
83    println!("--- Type Conversion Pipeline ---");
84    let parse_int = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
85    let double_int = BoxTransformer::new(|x: i32| x * 2);
86    let to_string = BoxTransformer::new(|x: i32| x.to_string());
87
88    let pipeline = parse_int.and_then(double_int).and_then(to_string);
89    println!(
90        "pipeline.apply('21') = {}",
91        pipeline.apply("21".to_string())
92    );
93    println!();
94
95    // Example 3: Shared transformation logic
96    println!("--- Shared Transformation Logic ---");
97    let square = ArcTransformer::new(|x: i32| x * x);
98
99    // Can be shared across different parts of the program
100    let transformer1 = square.clone();
101    let transformer2 = square.clone();
102
103    println!("transformer1.apply(5) = {}", transformer1.apply(5));
104    println!("transformer2.apply(7) = {}", transformer2.apply(7));
105    println!("square.apply(3) = {}", square.apply(3));
106    println!();
107
108    // Example 4: Transformer registry
109    println!("--- Transformer Registry ---");
110    let mut transformers: HashMap<String, RcTransformer<i32, String>> = HashMap::new();
111
112    transformers.insert(
113        "double".to_string(),
114        RcTransformer::new(|x: i32| format!("Doubled: {}", x * 2)),
115    );
116    transformers.insert(
117        "square".to_string(),
118        RcTransformer::new(|x: i32| format!("Squared: {}", x * x)),
119    );
120
121    if let Some(transformer) = transformers.get("double") {
122        println!("Transformer 'double': {}", transformer.apply(7));
123    }
124    if let Some(transformer) = transformers.get("square") {
125        println!("Transformer 'square': {}", transformer.apply(7));
126    }
127    println!();
128
129    // ====================================================================
130    // Part 5: Trait Usage
131    // ====================================================================
132    println!("=== Trait Usage ===\n");
133
134    fn apply_transformer<F: Transformer<i32, String>>(f: &F, x: i32) -> String {
135        f.apply(x)
136    }
137
138    let to_string = BoxTransformer::new(|x: i32| format!("Value: {}", x));
139    println!("Via trait: {}", apply_transformer(&to_string, 42));
140
141    println!("\n=== Demo Complete ===");
142}