qubit-function 0.8.1

Common functional programming type aliases for Rust, providing Java-style functional interfaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
# Transformer Design Analysis

## Overview

This document analyzes the main purposes and core values of Transformer from the perspective of its essential semantics, and explores reasonable design solutions.

The core function of Transformer is **to convert a value of one type to another type**, similar to Java's `Function<T, R>` interface and Rust standard library's `Fn(T) -> R`. This article will deeply analyze the design decisions of Transformer and propose solutions that meet actual business needs.

---

## I. Essential Semantics of Transformer

### 1.1 What is Transformer?

**Core semantics of Transformer (converter)**:

> **Convert a value of one type to another type. This is a "transformation" operation that consumes input to produce output, and should typically be a pure function (no side effects).**

This is similar to function mapping in mathematics:
- **Type conversion**: Map from one type to another type
-**Value consumption**: Consume ownership of input value during transformation
-**Pure function**: Same input should produce same output (from user perspective)
-**No side effects**: Do not modify external state (or hide through internal mutability)

**Comparison with other functional abstractions**:

| Type | Input | Output | Modify Input? | Modify Self? | Typical Use Cases |
|------|-------|--------|---------------|---------------|-------------------|
| **Transformer** | `T` | `R` ||| Type conversion, mapping, computation |
| **Predicate** | `&T` | `bool` ||| Filtering, validation, judgment |
| **Consumer** | `&T` | `()` ||| Observation, logging, statistics |
| **Supplier** | None | `T` | N/A || Factory, generator |

**Key insights**:
- Transformer's input is `T` (ownership transfer), not `&T` (borrowing)
- Transformer should be a "pure function" and should not modify its own state
- If state is needed (such as caching), use internal mutability

### 1.2 Main Uses of Transformer

| Use Case | Description | Example |
|----------|-------------|---------|
| **Type conversion** | Convert one type to another | `String -> i32`, `Vec<u8> -> String` |
| **Data mapping** | Work with `map()` and other iterator methods | `vec.into_iter().map(transformer)` |
| **Pipeline processing** | Build data processing pipelines | `parse.and_then(validate).and_then(transform)` |
| **Strategy pattern** | Save transformation logic as strategy | `transformers.insert("json", parser)` |
| **Lazy computation** | Save transformation logic, execute later | `let result = transformer.apply(input)` |

### 1.3 Core Value of Transformer

**Temporary conversion vs. saving logic**:

```rust
// ❌ No need for Transformer: convert once temporarily
let result = input.to_string();

// ✅ Need Transformer: save transformation logic for reuse
let to_string = BoxTransformer::new(|x: i32| x.to_string());
let result1 = values1.into_iter().map(|x| to_string.apply(x));
let result2 = values2.into_iter().map(|x| to_string.apply(x));
```

**The value of Transformer lies in**:
1. **Save transformation logic**: Encapsulate transformation operations as reusable objects
2. **Lazy execution**: Execute transformation only when needed
3. **Logic composition**: Build complex transformations through `and_then`, `compose`
4. **Simplify interfaces**: Improve code readability as type constraints

---

## II. Core Design Decisions

### 2.1 Input Parameters: T vs &T?

This is the most critical question in Transformer design.

#### Option A: Accept ownership `T` (Recommended)

```rust
pub trait Transformer<T, R> {
    fn transform(&self, input: T) -> R;  // Consume input ownership
}

// Use case: type conversion
let parse = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
let result = parse.apply("42".to_string());  // String is consumed
```

**Advantages**:
- **Conforms to transformation semantics**: Transformation typically consumes input (e.g., `String` to `Vec<u8>`)
-**Consistent with standard library**: `Option::map(FnOnce(T) -> U)` consumes T
-**Maximum flexibility**: Can move input, avoid unnecessary cloning
-**Clear semantics**: Transformer is "consume and transform"

**Disadvantages**:
- ⚠️ **Each call needs new input**: If you want to reuse input, you need to clone

#### Option B: Accept reference `&T`

```rust
pub trait Transformer<T, R> {
    fn transform(&self, input: &T) -> R;  // Borrow input
}

// Use case: non-destructive transformation
let length = BoxTransformer::new(|s: &String| s.len());
let s = "hello".to_string();
let len1 = length.apply(&s);
let len2 = length.apply(&s);  // Can use same input multiple times
```

**Advantages**:
- **Can reuse input**: Same input can be passed to multiple Transformers
-**Avoid cloning**: No need to clone input values

**Disadvantages**:
- **Doesn't conform to transformation semantics**: Real type conversion typically requires consuming input
-**Inconsistent with standard library**: Rust standard library's `map` consumes input
-**Limits flexibility**: Cannot get ownership of input in closure

#### Recommended Option: Use `T` (consume input)

**Reasons**:
1. **Conforms to Transformer's essence**: Converter transforms input to output
2. **Consistent with Rust standard library**: `Iterator::map`, `Option::map` all consume input
3. **Maximum flexibility**: Users can choose to move or clone
4. **Clear semantics**: Transformer is "converter", not "calculator"

**If you need scenarios that borrow input**:
- Use borrowing inside closure: `BoxTransformer::new(|x: String| x.len())`
- Or pass reference types: `BoxTransformer::new(|s: &str| s.len())`

### 2.2 Mutability of self: &self vs &mut self?

Transformer should be a pure function and should not modify its own state.

```rust
// ✅ Recommended: use &self
pub trait Transformer<T, R> {
    fn transform(&self, input: T) -> R;  // Don't modify self
}

// ❌ Not recommended: use &mut self
pub trait TransformerMut<T, R> {
    fn transform(&mut self, input: T) -> R;  // Can modify self
}
```

**Why don't we need TransformerMut?**

Consistent with Predicate analysis, internal mutability is sufficient to solve all "state needed" scenarios:

```rust
// Scenario: cache transformation results
use std::cell::RefCell;
use std::collections::HashMap;

let cache = RefCell::new(HashMap::new());
let cached_parse = BoxTransformer::new(move |s: String| {
    let mut cache = cache.borrow_mut();
    if let Some(&result) = cache.get(&s) {
        result
    } else {
        let result = s.parse::<i32>().unwrap_or(0);
        cache.insert(s, result);
        result
    }
});

// User doesn't need mut
cached_parse.apply("42".to_string());
```

**Why is internal mutability better?**

| Feature | TransformerMut (`&mut self`) | Transformer + RefCell (`&self`) |
|---------|-------------------------------|----------------------------------|
| **User code** | `let mut transformer = ...` | `let transformer = ...` |
| **Calling method** | `transformer.transform_mut(x)` | `transformer.apply(x)` |
| **Semantics** | "This transformer will change" ❌ | "This is a pure transformation" (internal optimization) ✅ |
| **Flexibility** | Cannot use in immutable context | Can use anywhere |
| **Implementation complexity** | Needs additional trait | Unified Transformer usage |

**Conclusion**: Only provide `Transformer<T, R>` (using `&self`), no need for `TransformerMut`.

### 2.3 Why do we need TransformerOnce? ✅

Similar to ConsumerOnce and SupplierOnce, the value of TransformerOnce lies in:

1. **Save FnOnce closures**: Closures can move captured variables
2. **One-time transformation**: Some transformation operations are inherently one-time
3. **Lazy execution**: Save transformation logic, execute later

```rust
pub trait TransformerOnce<T, R> {
    fn transform(self, input: T) -> R;  // Consume self and input
}

// Use case 1: Capture resources that can only be moved once
let resource = acquire_expensive_resource();
let transformer = BoxTransformerOnce::new(move |input: String| {
    // Use resource and input for transformation
    process_with_resource(resource, input)
});
let result = transformer.apply("data".to_string());  // transformer is consumed

// Use case 2: Lazy initialization transformation
struct Processor {
    initializer: Option<BoxTransformerOnce<Config, Processor>>,
}

impl Processor {
    fn initialize(mut self, config: Config) -> Processor {
        if let Some(init) = self.initializer.take() {
            init.apply(config)
        } else {
            self
        }
    }
}
```

**TransformerOnce vs Transformer**:

| | Transformer | TransformerOnce |
|---|---|---|
| **self signature** | `&self` | `self` |
| **Callable times** | Multiple | Once |
| **Closure type** | `Fn(T) -> R` | `FnOnce(T) -> R` |
| **Use cases** | Reusable transformation | One-time transformation, lazy computation |

**Conclusion**: TransformerOnce is **necessary**, complementing Transformer.

### 2.4 Output Parameters: R vs &R?

User suggestion "output should be concrete values rather than references" is completely correct.

```rust
// ✅ Recommended: return ownership
pub trait Transformer<T, R> {
    fn transform(&self, input: T) -> R;  // Return value ownership
}

// ❌ Not recommended: return reference (lifetime issues)
pub trait RefTransformer<'a, T, R> {
    fn transform(&'a self, input: T) -> &'a R;  // Complex lifetimes
}
```

**Why return `R`?**

1. **Avoid lifetime issues**: Returning references introduces complex lifetime constraints
2. **Conforms to transformation semantics**: Transformer generates new values, not returns references to existing values
3. **Flexibility**: Users can choose to return `Arc<T>`, `Rc<T>` and other smart pointers
4. **Consistent with standard library**: `Option::map` returns values, not references

### 2.5 Simplified Core Design

Based on the above analysis, the Transformer module only needs:

```rust
/// Transformer - converts input to output
pub trait Transformer<T, R> {
    /// Transform input, produce output
    ///
    /// Uses &self, can be called multiple times (but needs new input each time).
    /// If internal state is needed (like caching), use RefCell, Cell or Mutex.
    fn transform(&self, input: T) -> R;

    // Type conversion methods
    fn into_box(self) -> BoxTransformer<T, R> where ...;
    fn into_rc(self) -> RcTransformer<T, R> where ...;
    fn into_arc(self) -> ArcTransformer<T, R> where ...;

    /// Convert to standard closure for standard library integration
    fn into_fn(self) -> impl Fn(T) -> R where ...;
}

/// One-time transformer - can only be called once
pub trait TransformerOnce<T, R> {
    /// Transform input, consume self
    fn transform(self, input: T) -> R;

    fn into_box(self) -> BoxTransformerOnce<T, R> where ...;

    /// Convert to standard closure for standard library integration
    fn into_fn(self) -> impl FnOnce(T) -> R where ...;
}
```

**Just these two traits!** Simple, clear, semantic.

---

## III. Implementation Solution: Trait Abstraction + Multiple Implementations (Recommended)

Following Consumer, Supplier, Predicate design, adopt unified Trait + multiple implementations approach.

### 3.1 Core Architecture

```rust
// ============================================================================
// 1. Minimal Transformer trait
// ============================================================================

/// Transformer - converts input to output (repeatable calls)
pub trait Transformer<T, R> {
    /// Transform input value
    fn transform(&self, input: T) -> R;

    // Type conversion methods
    fn into_box(self) -> BoxTransformer<T, R> where Self: Sized + 'static, T: 'static, R: 'static;
    fn into_rc(self) -> RcTransformer<T, R> where Self: Sized + 'static, T: 'static, R: 'static;
    fn into_arc(self) -> ArcTransformer<T, R>
        where Self: Sized + Send + Sync + 'static, T: Send + 'static, R: Send + 'static;

    /// Convert to standard closure for standard library integration
    fn into_fn(self) -> impl Fn(T) -> R
        where Self: Sized + 'static, T: 'static, R: 'static;
}

/// One-time transformer - can only be called once
pub trait TransformerOnce<T, R> {
    /// Transform input value (consume self)
    fn transform(self, input: T) -> R;

    fn into_box(self) -> BoxTransformerOnce<T, R> where Self: Sized + 'static, T: 'static, R: 'static;

    /// Convert to standard closure for standard library integration
    fn into_fn(self) -> impl FnOnce(T) -> R
        where Self: Sized + 'static, T: 'static, R: 'static;
}

// ============================================================================
// 2. Provide extension capabilities for closures
// ============================================================================

/// Implement Transformer trait for closures
impl<T, R, F> Transformer<T, R> for F
where
    F: Fn(T) -> R
{
    fn transform(&self, input: T) -> R {
        self(input)
    }
    // ... into_* implementations
}

/// Extension trait providing composition methods for closures
pub trait FnTransformerOps<T, R>: Fn(T) -> R + Sized {
    /// Chain composition: self -> after
    fn and_then<S, G>(self, after: G) -> BoxTransformer<T, S>
    where
        G: Fn(R) -> S + 'static,
        T: 'static,
        S: 'static;

    /// Reverse composition: before -> self
    fn compose<S, G>(self, before: G) -> BoxTransformer<S, R>
    where
        G: Fn(S) -> T + 'static,
        S: 'static,
        R: 'static;
}

// ============================================================================
// 3. BoxTransformer - single ownership, repeatable calls
// ============================================================================

pub struct BoxTransformer<T, R> {
    function: Box<dyn Fn(T) -> R>,
}

impl<T, R> BoxTransformer<T, R>
where
    T: 'static,
    R: 'static,
{
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(T) -> R + 'static
    {
        BoxTransformer { function: Box::new(f) }
    }

    /// Identity transformation
    pub fn identity() -> BoxTransformer<T, T> {
        BoxTransformer::new(|x| x)
    }

    /// Constant transformation
    pub fn constant(value: R) -> BoxTransformer<T, R>
    where
        R: Clone
    {
        BoxTransformer::new(move |_| value.clone())
    }

    /// Chain composition: self -> after
    /// Consume self, return new BoxTransformer
    pub fn and_then<S, G>(self, after: G) -> BoxTransformer<T, S>
    where
        G: Transformer<R, S> + 'static,
        S: 'static,
    {
        let func = self.function;
        BoxTransformer::new(move |x| after.apply(func(x)))
    }

    /// Reverse composition: before -> self
    pub fn compose<S, G>(self, before: G) -> BoxTransformer<S, R>
    where
        G: Transformer<S, T> + 'static,
        S: 'static,
    {
        let func = self.function;
        BoxTransformer::new(move |x| func(before.apply(x)))
    }
}

impl<T, R> Transformer<T, R> for BoxTransformer<T, R> {
    fn transform(&self, input: T) -> R {
        (self.function)(input)
    }
    // ... into_* implementations
}

// ============================================================================
// 4. BoxTransformerOnce - one-time transformer
// ============================================================================

pub struct BoxTransformerOnce<T, R> {
    function: Option<Box<dyn FnOnce(T) -> R>>,
}

impl<T, R> BoxTransformerOnce<T, R>
where
    T: 'static,
    R: 'static,
{
    pub fn new<F>(f: F) -> Self
    where
        F: FnOnce(T) -> R + 'static
    {
        BoxTransformerOnce {
            function: Some(Box::new(f))
        }
    }

    /// Chain composition: self -> after
    pub fn and_then<S, G>(self, after: G) -> BoxTransformerOnce<T, S>
    where
        G: TransformerOnce<R, S> + 'static,
        S: 'static,
    {
        BoxTransformerOnce::new(move |x| {
            let intermediate = self.apply(x);
            after.apply(intermediate)
        })
    }

    /// Reverse composition: before -> self
    pub fn compose<S, G>(self, before: G) -> BoxTransformerOnce<S, R>
    where
        G: TransformerOnce<S, T> + 'static,
        S: 'static,
    {
        BoxTransformerOnce::new(move |x| {
            let intermediate = before.apply(x);
            self.apply(intermediate)
        })
    }
}

impl<T, R> TransformerOnce<T, R> for BoxTransformerOnce<T, R> {
    fn transform(mut self, input: T) -> R {
        (self.function.take().unwrap())(input)
    }
}

// ============================================================================
// 5. ArcTransformer - thread-safe shared ownership
// ============================================================================

pub struct ArcTransformer<T, R> {
    function: Arc<dyn Fn(T) -> R + Send + Sync>,
}

impl<T, R> ArcTransformer<T, R>
where
    T: Send + 'static,
    R: Send + 'static,
{
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(T) -> R + Send + Sync + 'static
    {
        ArcTransformer {
            function: Arc::new(f)
        }
    }

    /// Identity transformation
    pub fn identity() -> ArcTransformer<T, T>
    where
        T: Send + Sync
    {
        ArcTransformer::new(|x| x)
    }

    /// Chain composition: self -> after
    /// Borrow &self, return new ArcTransformer
    pub fn and_then<S, F>(&self, after: F) -> ArcTransformer<T, S>
    where
        S: Send + Sync + 'static,
        F: Transformer<R, S> + Send + Sync + 'static,
    {
        let self_func = Arc::clone(&self.function);
        ArcTransformer {
            function: Arc::new(move |x| after.apply(self_func(x))),
        }
    }

    /// Reverse composition: before -> self
    pub fn compose<S, F>(&self, before: F) -> ArcTransformer<S, R>
    where
        S: Send + Sync + 'static,
        F: Transformer<S, T> + Send + Sync + 'static,
    {
        let self_func = Arc::clone(&self.function);
        ArcTransformer {
            function: Arc::new(move |x| self_func(before.apply(x))),
        }
    }
}

impl<T, R> Transformer<T, R> for ArcTransformer<T, R>
where
    T: Send,
    R: Send,
{
    fn transform(&self, input: T) -> R {
        (self.function)(input)
    }
    // ... into_* implementations
}

impl<T, R> Clone for ArcTransformer<T, R> {
    fn clone(&self) -> Self {
        ArcTransformer {
            function: Arc::clone(&self.function),
        }
    }
}

// ============================================================================
// 6. RcTransformer - single-threaded shared ownership
// ============================================================================

pub struct RcTransformer<T, R> {
    function: Rc<dyn Fn(T) -> R>,
}

impl<T, R> RcTransformer<T, R>
where
    T: 'static,
    R: 'static,
{
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(T) -> R + 'static
    {
        RcTransformer {
            function: Rc::new(f)
        }
    }

    /// Identity transformation
    pub fn identity() -> RcTransformer<T, T> {
        RcTransformer::new(|x| x)
    }

    /// Chain composition: self -> after
    /// Borrow &self, return new RcTransformer
    pub fn and_then<S, F>(&self, after: F) -> RcTransformer<T, S>
    where
        S: 'static,
        F: Transformer<R, S> + 'static,
    {
        let self_func = Rc::clone(&self.function);
        RcTransformer {
            function: Rc::new(move |x| after.apply(self_func(x))),
        }
    }

    /// Reverse composition: before -> self
    pub fn compose<S, F>(&self, before: F) -> RcTransformer<S, R>
    where
        S: 'static,
        F: Transformer<S, T> + 'static,
    {
        let self_func = Rc::clone(&self.function);
        RcTransformer {
            function: Rc::new(move |x| self_func(before.apply(x))),
        }
    }
}

impl<T, R> Transformer<T, R> for RcTransformer<T, R> {
    fn transform(&self, input: T) -> R {
        (self.function)(input)
    }
    // ... into_* implementations
}

impl<T, R> Clone for RcTransformer<T, R> {
    fn clone(&self) -> Self {
        RcTransformer {
            function: Rc::clone(&self.function),
        }
    }
}
```

### 3.2 Usage Examples

```rust
// ============================================================================
// 1. Closures automatically have Transformer capability
// ============================================================================

let double = |x: i32| x * 2;
assert_eq!(double.apply(21), 42);  // Closure automatically implements Transformer

// Closures can be composed directly
let add_one = |x: i32| x + 1;
let pipeline = double.and_then(add_one);  // Returns BoxTransformer
assert_eq!(pipeline.apply(5), 11);  // (5 * 2) + 1

// ============================================================================
// 2. BoxTransformer - repeatable calls, single ownership
// ============================================================================

let parse = BoxTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));

// ✅ Can be called multiple times (needs new input each time)
assert_eq!(parse.apply("42".to_string()), 42);
assert_eq!(parse.apply("100".to_string()), 100);

// Method chaining
let pipeline = BoxTransformer::new(|s: String| s.len())
    .and_then(|len| len * 2)
    .and_then(|x| format!("Length: {}", x));

assert_eq!(pipeline.apply("hello".to_string()), "Length: 10");

// ============================================================================
// 3. BoxTransformerOnce - one-time use
// ============================================================================

// Capture resources that can only be moved once
let resource = vec![1, 2, 3];
let transformer = BoxTransformerOnce::new(move |multiplier: i32| {
    resource.into_iter().map(|x| x * multiplier).collect::<Vec<_>>()
});

let result = transformer.apply(10);
assert_eq!(result, vec![10, 20, 30]);
// transformer has been consumed, cannot be used again

// ============================================================================
// 4. ArcTransformer - multi-threaded sharing, doesn't consume ownership
// ============================================================================

let parse = ArcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));

// ✅ Can be cloned
let parse_clone = parse.clone();

// ✅ When composing, consumes parameter but not self (uses &self, parameter F)
let double = ArcTransformer::new(|x: i32| x * 2);
let pipeline = parse.and_then(double);

// Original parse transformer is still available (double has been consumed)
assert_eq!(parse.apply("42".to_string()), 42);
assert_eq!(pipeline.apply("21".to_string()), 42);

// ✅ Can be used across threads
use std::thread;
let handle = thread::spawn(move || {
    parse_clone.apply("100".to_string())
});
assert_eq!(handle.join().unwrap(), 100);

// ============================================================================
// 5. RcTransformer - single-threaded reuse, better performance
// ============================================================================

let parse = RcTransformer::new(|s: String| s.parse::<i32>().unwrap_or(0));
let double = RcTransformer::new(|x: i32| x * 2);

// ✅ Can be cloned
let parse_clone = parse.clone();

// ✅ When composing, consumes parameter but not self
let pipeline1 = parse.and_then(double);
let to_string = RcTransformer::new(|x: i32| x.to_string());
let pipeline2 = parse.and_then(to_string);

// Original parse transformer is still available (double and to_string have been consumed)
assert_eq!(parse.apply("42".to_string()), 42);

// ============================================================================
// 6. Unified interface - generic programming
// ============================================================================

fn transform_vec<T, R, F>(transformer: &F, vec: Vec<T>) -> Vec<R>
where
    F: Transformer<T, R>,
{
    vec.into_iter().map(|x| transformer.apply(x)).collect()
}

let arc_transformer = ArcTransformer::new(|x: i32| x * 2);
let results = transform_vec(&arc_transformer, vec![1, 2, 3]);
assert_eq!(results, vec![2, 4, 6]);

// ============================================================================
// 7. Using internal mutability for caching
// ============================================================================

use std::cell::RefCell;
use std::collections::HashMap;

let cache = RefCell::new(HashMap::new());
let cached_expensive = BoxTransformer::new(move |x: i32| {
    let mut cache = cache.borrow_mut();
    *cache.entry(x).or_insert_with(|| {
        // Simulate expensive computation
        println!("Computing for {}", x);
        x * x
    })
});

// First call: compute
assert_eq!(cached_expensive.apply(5), 25);  // Prints "Computing for 5"
// Second call: use cache
assert_eq!(cached_expensive.apply(5), 25);  // No print (uses cache)

// ============================================================================
// 8. Convert to standard closures - deep integration with standard library
// ============================================================================

let transformer = BoxTransformer::new(|x: i32| x * 2);

// Convert to standard closure, can be used directly in map methods
let func = transformer.into_fn();
let results: Vec<_> = vec![1, 2, 3].into_iter().map(func).collect();
assert_eq!(results, vec![2, 4, 6]);

// Can also be used directly
let transformer = BoxTransformer::new(|s: String| s.len());
let lengths: Vec<_> = vec!["hello".to_string(), "world".to_string()]
    .into_iter()
    .map(transformer.into_fn())
    .collect();
assert_eq!(lengths, vec![5, 5]);

// TransformerOnce can also be converted to FnOnce
let once_transformer = BoxTransformerOnce::new(|data: Vec<i32>| {
    data.into_iter().sum::<i32>()
});

let func_once = once_transformer.into_fn();
let result = func_once(vec![1, 2, 3, 4, 5]);
assert_eq!(result, 15);
```

### 3.3 Type Selection Guide

| Requirement | Recommended Type | Reason |
|-------------|------------------|---------|
| Repeatable calls, single ownership | `BoxTransformer` | Single ownership, can be called multiple times |
| One-time use | `BoxTransformerOnce` | Consume self, save FnOnce |
| Multi-threaded sharing | `ArcTransformer` | Thread-safe, cloneable |
| Single-threaded reuse | `RcTransformer` | No atomic operations, better performance |
| Need internal state (caching) | Any type + RefCell/Mutex | Internal mutability |

---

## IV. Comparison with Other Functional Abstractions

### 4.1 Core Differences

| | Transformer | Predicate | Consumer | Supplier |
|---|---|---|---|---|
| **Input** | `T` | `&T` | `&T` | None |
| **Output** | `R` | `bool` | `()` | `T` |
| **self signature** | `&self` | `&self` | `&mut self` | `&mut self` |
| **Consume input** |||| N/A |
| **Modify self** | ❌ (internal mutability) | ❌ (internal mutability) |||
| **Once variant** | ✅ Valuable | ❌ Meaningless | ✅ Valuable | ✅ Valuable |
| **Core purpose** | Type conversion, mapping | Filtering, validation | Observation, accumulation | Factory, generation |

### 4.2 Why does Transformer consume input while Predicate doesn't?

| | Transformer `T -> R` | Predicate `&T -> bool` |
|---|---|---|
| **Semantics** | "Transform" - convert input to output | "Judge" - check if input meets condition |
| **Typical scenarios** | `String -> Vec<u8>` | `&i32 -> bool` (is > 0) |
| **Input afterwards** | Input has been transformed, usually no longer needed | After judgment, input still exists |
| **Ownership** | Need ownership for transformation | Only need to read, don't need ownership |

**Difference in actual business**:

```rust
// Transformer: Transform - consume input
let parse_json = BoxTransformer::new(|json_str: String| {
    serde_json::from_str(&json_str).unwrap()  // Consume String
});

// Predicate: Judge - borrow input
let is_valid_json = BoxPredicate::new(|json_str: &String| {
    serde_json::from_str::<serde_json::Value>(json_str).is_ok()  // Only borrow
});

let json = r#"{"key": "value"}"#.to_string();

// After judgment, json is still available
if is_valid_json.test(&json) {
    let data = parse_json.apply(json);  // json is consumed
    // json is no longer available
}
```

### 4.3 Design Consistency

All functional abstractions follow unified design patterns:

1. **Unified trait interfaces**: Each abstraction has core traits
2. **Three implementations**: Box (single), Arc (shared + thread-safe), Rc (shared + single-threaded)
3. **Type-preserving method chaining**: Composition methods return same type
4. **Closures automatically implement traits**: Seamless integration
5. **Extension traits provide composition capabilities**: Like `FnTransformerOps`

---

## V. Real Business Scenario Examples

### 5.1 Data Transformation Pipeline

```rust
// Build complex data processing pipeline
let pipeline = BoxTransformer::new(|raw: String| raw.trim().to_string())
    .and_then(|s| s.parse::<i32>().ok())
    .and_then(|opt| opt.unwrap_or(0))
    .and_then(|x| x * 2)
    .and_then(|x| format!("Result: {}", x));

let result = pipeline.apply("  42  ".to_string());
assert_eq!(result, "Result: 84");
```

### 5.2 Configuration Transformer

```rust
use std::collections::HashMap;

struct ConfigManager {
    transformers: HashMap<String, BoxTransformer<String, String>>,
}

impl ConfigManager {
    fn new() -> Self {
        let mut transformers = HashMap::new();

        // Register various transformers
        transformers.insert(
            "uppercase".to_string(),
            BoxTransformer::new(|s: String| s.to_uppercase()),
        );

        transformers.insert(
            "trim".to_string(),
            BoxTransformer::new(|s: String| s.trim().to_string()),
        );

        ConfigManager { transformers }
    }

    fn transform(&self, key: &str, value: String) -> String {
        if let Some(transformer) = self.transformers.get(key) {
            transformer.apply(value)
        } else {
            value
        }
    }
}
```

### 5.3 Multi-threaded Data Processing

```rust
use std::thread;

// Create transformer that can be shared across threads
let heavy_transform = ArcTransformer::new(|data: Vec<u8>| {
    // Simulate time-consuming transformation operation
    data.into_iter().map(|b| b.wrapping_mul(2)).collect::<Vec<_>>()
});

let mut handles = vec![];

for i in 0..4 {
    let transformer = heavy_transform.clone();
    let handle = thread::spawn(move || {
        let data = vec![i; 100];
        transformer.apply(data)
    });
    handles.push(handle);
}

let results: Vec<_> = handles.into_iter()
    .map(|h| h.join().unwrap())
    .collect();
```

### 5.4 Lazy Computation

```rust
// Save expensive transformation logic, execute lazily
struct LazyComputation<T, R> {
    input: Option<T>,
    transformer: BoxTransformerOnce<T, R>,
}

impl<T, R> LazyComputation<T, R> {
    fn new<F>(input: T, transformer: F) -> Self
    where
        F: FnOnce(T) -> R + 'static,
        T: 'static,
        R: 'static,
    {
        LazyComputation {
            input: Some(input),
            transformer: BoxTransformerOnce::new(transformer),
        }
    }

    fn compute(mut self) -> R {
        let input = self.input.take().unwrap();
        self.transformer.apply(input)
    }
}

// Usage
let lazy = LazyComputation::new(
    "large dataset".to_string(),
    |data| {
        // Only execute when compute() is called
        expensive_analysis(data)
    },
);

// Execute later
let result = lazy.compute();
```

---

## VI. Summary

### 6.1 Core Design Principles

1. **Transformer consumes input `T`**: Conforms to transformation semantics, maximum flexibility
2. **Transformer returns ownership `R`**: Avoid lifetime issues, clear semantics
3. **Transformer uses `&self`**: Pure function, doesn't modify self (use internal mutability)
4. **Keep TransformerOnce**: One-time transformation, lazy computation
5. **No need for TransformerMut**: Internal mutability is sufficient
6. **Type names are semantically clear**: Box/Arc/Rc express ownership models

### 6.2 Why is this design the best?

**Comparison with over-engineering**:

| | Over-engineering | Simplified Design (Recommended) |
|---|---|---|
| **Number of traits** | Multiple (Function, FunctionMut, RefFunction) | 2 (Transformer, TransformerOnce) ✅ |
| **Input types** | Confusing (T, &T, &mut T) | Clear (T) ✅ |
| **User mental burden** | High (which one to use?) | Low (clear semantics) ✅ |
| **State management** | Need `&mut self` | Internal mutability ✅ |
| **API consistency** | Multiple sets of methods | Unified transform ✅ |

**Consistency with other module designs**:

- Consumer **observes** input (`&T`), **can modify** self (accumulation)
- Predicate **judges** input (`&T`), **doesn't modify** self (pure function)
- Transformer **transforms** input (`T`), **doesn't modify** self (pure function)
- Supplier **generates** output (no input), **can modify** self (state increment)

### 6.3 Why is Transformer better than Function?

| Aspect | Function | Transformer |
|--------|----------|-------------|
| **Semantic precision** | "Function" - too broad ❌ | "Transformer" - precisely expresses transformation ✅ |
| **Avoid confusion** | Confusing with Fn/FnMut/FnOnce ❌ | Completely distinct ✅ |
| **Naming symmetry** | Inconsistent with other modules ❌ | Symmetric with Consumer, Supplier ✅ |
| **Readability** | `BoxFunction<String, User>`| `BoxTransformer<String, User>`|
| **Industry practice** | Not clear enough ❌ | Conforms to Kotlin, ReactiveX, etc. ✅ |

### 6.4 Final Conclusion

For a library project like `qubit-function`:

1. **Adopt Trait + multiple implementations approach**: Unified interface, flexible implementation
2. **Provide Transformer and TransformerOnce**: Cover repeatable calls and one-time use scenarios
3. **Three implementations**: BoxTransformer, ArcTransformer, RcTransformer
4. **Use internal mutability**: Use RefCell/Cell/Mutex when state is needed
5. **Document best practices**: Guide users on when to use which type

This design:
- **Conforms to transformation semantics**: Transformer consumes input to produce output
-**Consistent with Rust standard library**: `Iterator::map` etc. all consume input
-**Maximum flexibility**: Repeatable calls (Transformer) and one-time use (TransformerOnce)
-**Simple and elegant**: Only two core traits, clear and concise
-**Precise naming**: Transformer better expresses "transformation" semantics than Function
-**Long-term maintainable**: Clear architecture, clear semantics

**This is an elegant solution that starts from real business needs, is carefully considered, and conforms to Rust conventions.**