prism3_function/
transformer.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025.
4 *    3-Prism Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Transformer Types
10//!
11//! Provides Rust implementations of transformer traits for type conversion
12//! and value transformation. Transformers consume input values (taking
13//! ownership) and produce output values.
14//!
15//! This module provides the `Transformer<T, R>` trait and three
16//! implementations:
17//!
18//! - [`BoxTransformer`]: Single ownership, not cloneable
19//! - [`ArcTransformer`]: Thread-safe shared ownership, cloneable
20//! - [`RcTransformer`]: Single-threaded shared ownership, cloneable
21//!
22//! # Author
23//!
24//! Hu Haixing
25
26use std::rc::Rc;
27use std::sync::Arc;
28
29use crate::predicate::{ArcPredicate, BoxPredicate, Predicate, RcPredicate};
30
31// ============================================================================
32// Core Trait
33// ============================================================================
34
35/// Transformer trait - transforms values from type T to type R
36///
37/// Defines the behavior of a transformation: converting a value of type `T`
38/// to a value of type `R` by consuming the input. This is analogous to
39/// `Fn(T) -> R` in Rust's standard library.
40///
41/// # Type Parameters
42///
43/// * `T` - The type of the input value (consumed)
44/// * `R` - The type of the output value
45///
46/// # Author
47///
48/// Hu Haixing
49pub trait Transformer<T, R> {
50    /// Applies the transformation to the input value to produce an output value
51    ///
52    /// # Parameters
53    ///
54    /// * `input` - The input value to transform (consumed)
55    ///
56    /// # Returns
57    ///
58    /// The transformed output value
59    fn apply(&self, input: T) -> R;
60
61    /// Converts to BoxTransformer
62    ///
63    /// **⚠️ Consumes `self`**: The original transformer becomes
64    /// unavailable after calling this method.
65    ///
66    /// # Default Implementation
67    ///
68    /// The default implementation wraps `self` in a `Box` and creates a
69    /// `BoxTransformer`. Types can override this method to provide more
70    /// efficient conversions.
71    ///
72    /// # Returns
73    ///
74    /// Returns `BoxTransformer<T, R>`
75    fn into_box(self) -> BoxTransformer<T, R>
76    where
77        Self: Sized + 'static,
78        T: 'static,
79        R: 'static,
80    {
81        BoxTransformer::new(move |x| self.apply(x))
82    }
83
84    /// Converts to RcTransformer
85    ///
86    /// **⚠️ Consumes `self`**: The original transformer becomes
87    /// unavailable after calling this method.
88    ///
89    /// # Default Implementation
90    ///
91    /// The default implementation wraps `self` in an `Rc` and creates an
92    /// `RcTransformer`. Types can override this method to provide more
93    /// efficient conversions.
94    ///
95    /// # Returns
96    ///
97    /// Returns `RcTransformer<T, R>`
98    fn into_rc(self) -> RcTransformer<T, R>
99    where
100        Self: Sized + 'static,
101        T: 'static,
102        R: 'static,
103    {
104        RcTransformer::new(move |x| self.apply(x))
105    }
106
107    /// Converts to ArcTransformer
108    ///
109    /// **⚠️ Consumes `self`**: The original transformer becomes
110    /// unavailable after calling this method.
111    ///
112    /// # Default Implementation
113    ///
114    /// The default implementation wraps `self` in an `Arc` and creates
115    /// an `ArcTransformer`. Types can override this method to provide
116    /// more efficient conversions.
117    ///
118    /// # Returns
119    ///
120    /// Returns `ArcTransformer<T, R>`
121    fn into_arc(self) -> ArcTransformer<T, R>
122    where
123        Self: Sized + Send + Sync + 'static,
124        T: Send + Sync + 'static,
125        R: Send + Sync + 'static,
126    {
127        ArcTransformer::new(move |x| self.apply(x))
128    }
129
130    /// Converts transformer to a closure
131    ///
132    /// **⚠️ Consumes `self`**: The original transformer becomes
133    /// unavailable after calling this method.
134    ///
135    /// # Default Implementation
136    ///
137    /// The default implementation creates a closure that captures `self`
138    /// and calls its `transform` method. Types can override this method
139    /// to provide more efficient conversions.
140    ///
141    /// # Returns
142    ///
143    /// Returns a closure that implements `Fn(T) -> R`
144    fn into_fn(self) -> impl Fn(T) -> R
145    where
146        Self: Sized + 'static,
147        T: 'static,
148        R: 'static,
149    {
150        move |t: T| self.apply(t)
151    }
152
153    /// Converts to BoxTransformer without consuming self
154    ///
155    /// **📌 Borrows `&self`**: The original transformer remains usable
156    /// after calling this method.
157    ///
158    /// # Default Implementation
159    ///
160    /// The default implementation creates a new `BoxTransformer` that
161    /// captures a reference-counted clone. Types implementing `Clone`
162    /// can override this method to provide more efficient conversions.
163    ///
164    /// # Returns
165    ///
166    /// Returns `BoxTransformer<T, R>`
167    ///
168    /// # Examples
169    ///
170    /// ```rust
171    /// use prism3_function::{ArcTransformer, Transformer};
172    ///
173    /// let double = ArcTransformer::new(|x: i32| x * 2);
174    /// let boxed = double.to_box();
175    ///
176    /// // Original transformer still usable
177    /// assert_eq!(double.apply(21), 42);
178    /// assert_eq!(boxed.apply(21), 42);
179    /// ```
180    fn to_box(&self) -> BoxTransformer<T, R>
181    where
182        Self: Clone + 'static,
183        T: 'static,
184        R: 'static,
185    {
186        self.clone().into_box()
187    }
188
189    /// Converts to RcTransformer without consuming self
190    ///
191    /// **📌 Borrows `&self`**: The original transformer remains usable
192    /// after calling this method.
193    ///
194    /// # Default Implementation
195    ///
196    /// The default implementation creates a new `RcTransformer` that
197    /// captures a reference-counted clone. Types implementing `Clone`
198    /// can override this method to provide more efficient conversions.
199    ///
200    /// # Returns
201    ///
202    /// Returns `RcTransformer<T, R>`
203    ///
204    /// # Examples
205    ///
206    /// ```rust
207    /// use prism3_function::{ArcTransformer, Transformer};
208    ///
209    /// let double = ArcTransformer::new(|x: i32| x * 2);
210    /// let rc = double.to_rc();
211    ///
212    /// // Original transformer still usable
213    /// assert_eq!(double.apply(21), 42);
214    /// assert_eq!(rc.apply(21), 42);
215    /// ```
216    fn to_rc(&self) -> RcTransformer<T, R>
217    where
218        Self: Clone + 'static,
219        T: 'static,
220        R: 'static,
221    {
222        self.clone().into_rc()
223    }
224
225    /// Converts to ArcTransformer without consuming self
226    ///
227    /// **📌 Borrows `&self`**: The original transformer remains usable
228    /// after calling this method.
229    ///
230    /// # Default Implementation
231    ///
232    /// The default implementation creates a new `ArcTransformer` that
233    /// captures a reference-counted clone. Types implementing `Clone`
234    /// can override this method to provide more efficient conversions.
235    ///
236    /// # Returns
237    ///
238    /// Returns `ArcTransformer<T, R>`
239    ///
240    /// # Examples
241    ///
242    /// ```rust
243    /// use prism3_function::{ArcTransformer, Transformer};
244    ///
245    /// let double = ArcTransformer::new(|x: i32| x * 2);
246    /// let arc = double.to_arc();
247    ///
248    /// // Original transformer still usable
249    /// assert_eq!(double.apply(21), 42);
250    /// assert_eq!(arc.apply(21), 42);
251    /// ```
252    fn to_arc(&self) -> ArcTransformer<T, R>
253    where
254        Self: Clone + Send + Sync + 'static,
255        T: Send + Sync + 'static,
256        R: Send + Sync + 'static,
257    {
258        self.clone().into_arc()
259    }
260
261    /// Converts transformer to a closure without consuming self
262    ///
263    /// **📌 Borrows `&self`**: The original transformer remains usable
264    /// after calling this method.
265    ///
266    /// # Default Implementation
267    ///
268    /// The default implementation creates a closure that captures a
269    /// clone of `self` and calls its `transform` method. Types can
270    /// override this method to provide more efficient conversions.
271    ///
272    /// # Returns
273    ///
274    /// Returns a closure that implements `Fn(T) -> R`
275    ///
276    /// # Examples
277    ///
278    /// ```rust
279    /// use prism3_function::{ArcTransformer, Transformer};
280    ///
281    /// let double = ArcTransformer::new(|x: i32| x * 2);
282    /// let closure = double.to_fn();
283    ///
284    /// // Original transformer still usable
285    /// assert_eq!(double.apply(21), 42);
286    /// assert_eq!(closure(21), 42);
287    /// ```
288    fn to_fn(&self) -> impl Fn(T) -> R
289    where
290        Self: Clone + 'static,
291        T: 'static,
292        R: 'static,
293    {
294        self.clone().into_fn()
295    }
296}
297
298// ============================================================================
299// BoxTransformer - Box<dyn Fn(T) -> R>
300// ============================================================================
301
302/// BoxTransformer - transformer wrapper based on `Box<dyn Fn>`
303///
304/// A transformer wrapper that provides single ownership with reusable
305/// transformation. The transformer consumes the input and can be called
306/// multiple times.
307///
308/// # Features
309///
310/// - **Based on**: `Box<dyn Fn(T) -> R>`
311/// - **Ownership**: Single ownership, cannot be cloned
312/// - **Reusability**: Can be called multiple times (each call consumes its
313///   input)
314/// - **Thread Safety**: Not thread-safe (no `Send + Sync` requirement)
315///
316/// # Author
317///
318/// Hu Haixing
319pub struct BoxTransformer<T, R> {
320    function: Box<dyn Fn(T) -> R>,
321}
322
323impl<T, R> BoxTransformer<T, R>
324where
325    T: 'static,
326    R: 'static,
327{
328    /// Creates a new BoxTransformer
329    ///
330    /// # Parameters
331    ///
332    /// * `f` - The closure or function to wrap
333    ///
334    /// # Examples
335    ///
336    /// ```rust
337    /// use prism3_function::{BoxTransformer, Transformer};
338    ///
339    /// let double = BoxTransformer::new(|x: i32| x * 2);
340    /// assert_eq!(double.apply(21), 42);
341    /// ```
342    pub fn new<F>(f: F) -> Self
343    where
344        F: Fn(T) -> R + 'static,
345    {
346        BoxTransformer {
347            function: Box::new(f),
348        }
349    }
350
351    /// Creates an identity transformer
352    ///
353    /// # Examples
354    ///
355    /// ```rust
356    /// use prism3_function::{BoxTransformer, Transformer};
357    ///
358    /// let identity = BoxTransformer::<i32, i32>::identity();
359    /// assert_eq!(identity.apply(42), 42);
360    /// ```
361    pub fn identity() -> BoxTransformer<T, T> {
362        BoxTransformer::new(|x| x)
363    }
364
365    /// Chain composition - applies self first, then after
366    ///
367    /// Creates a new transformer that applies this transformer first, then
368    /// applies the after transformer to the result. Consumes self.
369    ///
370    /// # Type Parameters
371    ///
372    /// * `S` - The output type of the after transformer
373    /// * `F` - The type of the after transformer (must implement
374    ///   Transformer<R, S>)
375    ///
376    /// # Parameters
377    ///
378    /// * `after` - The transformer to apply after self. **Note: This parameter
379    ///   is passed by value and will transfer ownership.** If you need to
380    ///   preserve the original transformer, clone it first (if it implements
381    ///   `Clone`). Can be:
382    ///   - A closure: `|x: R| -> S`
383    ///   - A function pointer: `fn(R) -> S`
384    ///   - A `BoxTransformer<R, S>`
385    ///   - An `RcTransformer<R, S>`
386    ///   - An `ArcTransformer<R, S>`
387    ///   - Any type implementing `Transformer<R, S>`
388    ///
389    /// # Returns
390    ///
391    /// A new BoxTransformer representing the composition
392    ///
393    /// # Examples
394    ///
395    /// ## Direct value passing (ownership transfer)
396    ///
397    /// ```rust
398    /// use prism3_function::{BoxTransformer, Transformer};
399    ///
400    /// let double = BoxTransformer::new(|x: i32| x * 2);
401    /// let to_string = BoxTransformer::new(|x: i32| x.to_string());
402    ///
403    /// // to_string is moved here
404    /// let composed = double.and_then(to_string);
405    /// assert_eq!(composed.apply(21), "42");
406    /// // to_string.apply(5); // Would not compile - moved
407    /// ```
408    ///
409    /// ## Preserving original with clone
410    ///
411    /// ```rust
412    /// use prism3_function::{BoxTransformer, Transformer};
413    ///
414    /// let double = BoxTransformer::new(|x: i32| x * 2);
415    /// let to_string = BoxTransformer::new(|x: i32| x.to_string());
416    ///
417    /// // Clone to preserve original
418    /// let composed = double.and_then(to_string.clone());
419    /// assert_eq!(composed.apply(21), "42");
420    ///
421    /// // Original still usable
422    /// assert_eq!(to_string.apply(5), "5");
423    /// ```
424    pub fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>
425    where
426        S: 'static,
427        F: Transformer<R, S> + 'static,
428    {
429        let self_fn = self.function;
430        BoxTransformer::new(move |x: T| after.apply(self_fn(x)))
431    }
432
433    /// Reverse composition - applies before first, then self
434    ///
435    /// Creates a new transformer that applies the before transformer first,
436    /// then applies this transformer to the result. Consumes self.
437    ///
438    /// # Type Parameters
439    ///
440    /// * `S` - The input type of the before transformer
441    /// * `F` - The type of the before transformer (must implement
442    ///   Transformer<S, T>)
443    ///
444    /// # Parameters
445    ///
446    /// * `before` - The transformer to apply before self. **Note: This parameter
447    ///   is passed by value and will transfer ownership.** If you need to
448    ///   preserve the original transformer, clone it first (if it implements
449    ///   `Clone`). Can be:
450    ///   - A closure: `|x: S| -> T`
451    ///   - A function pointer: `fn(S) -> T`
452    ///   - A `BoxTransformer<S, T>`
453    ///   - An `RcTransformer<S, T>`
454    ///   - An `ArcTransformer<S, T>`
455    ///   - Any type implementing `Transformer<S, T>`
456    ///
457    /// # Returns
458    ///
459    /// A new BoxTransformer representing the composition
460    ///
461    /// # Examples
462    ///
463    /// ## Direct value passing (ownership transfer)
464    ///
465    /// ```rust
466    /// use prism3_function::{BoxTransformer, Transformer};
467    ///
468    /// let double = BoxTransformer::new(|x: i32| x * 2);
469    /// let add_one = BoxTransformer::new(|x: i32| x + 1);
470    ///
471    /// // add_one is moved here
472    /// let composed = double.compose(add_one);
473    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
474    /// // add_one.apply(3); // Would not compile - moved
475    /// ```
476    ///
477    /// ## Preserving original with clone
478    ///
479    /// ```rust
480    /// use prism3_function::{BoxTransformer, Transformer};
481    ///
482    /// let double = BoxTransformer::new(|x: i32| x * 2);
483    /// let add_one = BoxTransformer::new(|x: i32| x + 1);
484    ///
485    /// // Clone to preserve original
486    /// let composed = double.compose(add_one.clone());
487    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
488    ///
489    /// // Original still usable
490    /// assert_eq!(add_one.apply(3), 4);
491    /// ```
492    pub fn compose<S, F>(self, before: F) -> BoxTransformer<S, R>
493    where
494        S: 'static,
495        F: Transformer<S, T> + 'static,
496    {
497        let self_fn = self.function;
498        BoxTransformer::new(move |x: S| self_fn(before.apply(x)))
499    }
500
501    /// Creates a conditional transformer
502    ///
503    /// Returns a transformer that only executes when a predicate is satisfied.
504    /// You must call `or_else()` to provide an alternative transformer for when
505    /// the condition is not satisfied.
506    ///
507    /// # Parameters
508    ///
509    /// * `predicate` - The condition to check. **Note: This parameter is passed
510    ///   by value and will transfer ownership.** If you need to preserve the
511    ///   original predicate, clone it first (if it implements `Clone`). Can be:
512    ///   - A closure: `|x: &T| -> bool`
513    ///   - A function pointer: `fn(&T) -> bool`
514    ///   - A `BoxPredicate<T>`
515    ///   - An `RcPredicate<T>`
516    ///   - An `ArcPredicate<T>`
517    ///   - Any type implementing `Predicate<T>`
518    ///
519    /// # Returns
520    ///
521    /// Returns `BoxConditionalTransformer<T, R>`
522    ///
523    /// # Examples
524    ///
525    /// ## Basic usage with or_else
526    ///
527    /// ```rust
528    /// use prism3_function::{Transformer, BoxTransformer};
529    ///
530    /// let double = BoxTransformer::new(|x: i32| x * 2);
531    /// let identity = BoxTransformer::<i32, i32>::identity();
532    /// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
533    ///
534    /// assert_eq!(conditional.apply(5), 10);
535    /// assert_eq!(conditional.apply(-5), -5); // identity
536    /// ```
537    ///
538    /// ## Preserving predicate with clone
539    ///
540    /// ```rust
541    /// use prism3_function::{Transformer, BoxTransformer, BoxPredicate};
542    ///
543    /// let double = BoxTransformer::new(|x: i32| x * 2);
544    /// let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
545    ///
546    /// // Clone to preserve original predicate
547    /// let conditional = double.when(is_positive.clone())
548    ///     .or_else(BoxTransformer::identity());
549    ///
550    /// assert_eq!(conditional.apply(5), 10);
551    ///
552    /// // Original predicate still usable
553    /// assert!(is_positive.test(&3));
554    /// ```
555    pub fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>
556    where
557        P: Predicate<T> + 'static,
558    {
559        BoxConditionalTransformer {
560            transformer: self,
561            predicate: predicate.into_box(),
562        }
563    }
564}
565
566impl<T, R> BoxTransformer<T, R>
567where
568    T: 'static,
569    R: Clone + 'static,
570{
571    /// Creates a constant transformer
572    ///
573    /// # Examples
574    ///
575    /// ```rust
576    /// use prism3_function::{BoxTransformer, Transformer};
577    ///
578    /// let constant = BoxTransformer::constant("hello");
579    /// assert_eq!(constant.apply(123), "hello");
580    /// ```
581    pub fn constant(value: R) -> BoxTransformer<T, R> {
582        BoxTransformer::new(move |_| value.clone())
583    }
584}
585
586impl<T, R> Transformer<T, R> for BoxTransformer<T, R> {
587    fn apply(&self, input: T) -> R {
588        (self.function)(input)
589    }
590
591    // Override with zero-cost implementation: directly return itself
592    fn into_box(self) -> BoxTransformer<T, R>
593    where
594        T: 'static,
595        R: 'static,
596    {
597        self
598    }
599
600    // Override with optimized implementation: convert Box to Rc
601    fn into_rc(self) -> RcTransformer<T, R>
602    where
603        T: 'static,
604        R: 'static,
605    {
606        RcTransformer {
607            function: Rc::from(self.function),
608        }
609    }
610
611    // do NOT override BoxTransformer::into_arc() because BoxTransformer is not Send + Sync
612    // and calling BoxTransformer::to_arc() will cause a compile error
613
614    // Override with optimized implementation: directly return the
615    // underlying function by unwrapping the Box
616    fn into_fn(self) -> impl Fn(T) -> R
617    where
618        T: 'static,
619        R: 'static,
620    {
621        self.function
622    }
623
624    // Note: BoxTransformer doesn't implement Clone, so the default to_xxx()
625    // implementations that require Clone cannot be used. We need to provide
626    // special implementations that create new transformers by wrapping the
627    // function reference.
628
629    // Override: BoxTransformer doesn't implement Clone, can't use default
630    // We create a new BoxTransformer that references self through a closure
631    // This requires T and R to be Clone-independent
632    // Users should prefer using RcTransformer if they need sharing
633
634    // Note: We intentionally don't override to_box(), to_rc(), to_arc(), to_fn()
635    // for BoxTransformer because:
636    // 1. BoxTransformer doesn't implement Clone
637    // 2. We can't share ownership of Box<dyn Fn> without cloning
638    // 3. Users should convert to RcTransformer or ArcTransformer first if they
639    //    need to create multiple references
640    // 4. The default implementations will fail to compile (as expected), which
641    //    guides users to the correct usage pattern
642}
643
644// ============================================================================
645// BoxConditionalTransformer - Box-based Conditional Transformer
646// ============================================================================
647
648/// BoxConditionalTransformer struct
649///
650/// A conditional transformer that only executes when a predicate is satisfied.
651/// Uses `BoxTransformer` and `BoxPredicate` for single ownership semantics.
652///
653/// This type is typically created by calling `BoxTransformer::when()` and is
654/// designed to work with the `or_else()` method to create if-then-else logic.
655///
656/// # Features
657///
658/// - **Single Ownership**: Not cloneable, consumes `self` on use
659/// - **Conditional Execution**: Only transforms when predicate returns `true`
660/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
661/// - **Implements Transformer**: Can be used anywhere a `Transformer` is expected
662///
663/// # Examples
664///
665/// ## With or_else Branch
666///
667/// ```rust
668/// use prism3_function::{Transformer, BoxTransformer};
669///
670/// let double = BoxTransformer::new(|x: i32| x * 2);
671/// let negate = BoxTransformer::new(|x: i32| -x);
672/// let conditional = double.when(|x: &i32| *x > 0).or_else(negate);
673///
674/// assert_eq!(conditional.apply(5), 10); // when branch executed
675/// assert_eq!(conditional.apply(-5), 5); // or_else branch executed
676/// ```
677///
678/// # Author
679///
680/// Haixing Hu
681pub struct BoxConditionalTransformer<T, R> {
682    transformer: BoxTransformer<T, R>,
683    predicate: BoxPredicate<T>,
684}
685
686impl<T, R> BoxConditionalTransformer<T, R>
687where
688    T: 'static,
689    R: 'static,
690{
691    /// Adds an else branch
692    ///
693    /// Executes the original transformer when the condition is satisfied,
694    /// otherwise executes else_transformer.
695    ///
696    /// # Parameters
697    ///
698    /// * `else_transformer` - The transformer for the else branch, can be:
699    ///   - Closure: `|x: T| -> R`
700    ///   - `BoxTransformer<T, R>`, `RcTransformer<T, R>`, `ArcTransformer<T, R>`
701    ///   - Any type implementing `Transformer<T, R>`
702    ///
703    /// # Returns
704    ///
705    /// Returns the composed `BoxTransformer<T, R>`
706    ///
707    /// # Examples
708    ///
709    /// ## Using a closure (recommended)
710    ///
711    /// ```rust
712    /// use prism3_function::{Transformer, BoxTransformer};
713    ///
714    /// let double = BoxTransformer::new(|x: i32| x * 2);
715    /// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
716    ///
717    /// assert_eq!(conditional.apply(5), 10); // Condition satisfied, execute double
718    /// assert_eq!(conditional.apply(-5), 5); // Condition not satisfied, execute negate
719    /// ```
720    pub fn or_else<F>(self, else_transformer: F) -> BoxTransformer<T, R>
721    where
722        F: Transformer<T, R> + 'static,
723    {
724        let pred = self.predicate;
725        let then_trans = self.transformer;
726        BoxTransformer::new(move |t| {
727            if pred.test(&t) {
728                then_trans.apply(t)
729            } else {
730                else_transformer.apply(t)
731            }
732        })
733    }
734}
735
736// ============================================================================
737// ArcTransformer - Arc<dyn Fn(T) -> R + Send + Sync>
738// ============================================================================
739
740/// ArcTransformer - thread-safe transformer wrapper
741///
742/// A thread-safe, clonable transformer wrapper suitable for multi-threaded
743/// scenarios. Can be called multiple times and shared across threads.
744///
745/// # Features
746///
747/// - **Based on**: `Arc<dyn Fn(T) -> R + Send + Sync>`
748/// - **Ownership**: Shared ownership via reference counting
749/// - **Reusability**: Can be called multiple times (each call consumes its
750///   input)
751/// - **Thread Safety**: Thread-safe (`Send + Sync` required)
752/// - **Clonable**: Cheap cloning via `Arc::clone`
753///
754/// # Author
755///
756/// Hu Haixing
757pub struct ArcTransformer<T, R> {
758    function: Arc<dyn Fn(T) -> R + Send + Sync>,
759}
760
761impl<T, R> ArcTransformer<T, R>
762where
763    T: Send + Sync + 'static,
764    R: 'static,
765{
766    /// Creates a new ArcTransformer
767    ///
768    /// # Parameters
769    ///
770    /// * `f` - The closure or function to wrap (must be Send + Sync)
771    ///
772    /// # Examples
773    ///
774    /// ```rust
775    /// use prism3_function::{ArcTransformer, Transformer};
776    ///
777    /// let double = ArcTransformer::new(|x: i32| x * 2);
778    /// assert_eq!(double.apply(21), 42);
779    /// ```
780    pub fn new<F>(f: F) -> Self
781    where
782        F: Fn(T) -> R + Send + Sync + 'static,
783    {
784        ArcTransformer {
785            function: Arc::new(f),
786        }
787    }
788
789    /// Creates an identity transformer
790    ///
791    /// # Examples
792    ///
793    /// ```rust
794    /// use prism3_function::{ArcTransformer, Transformer};
795    ///
796    /// let identity = ArcTransformer::<i32, i32>::identity();
797    /// assert_eq!(identity.apply(42), 42);
798    /// ```
799    pub fn identity() -> ArcTransformer<T, T> {
800        ArcTransformer::new(|x| x)
801    }
802
803    /// Chain composition - applies self first, then after
804    ///
805    /// Creates a new transformer that applies this transformer first, then
806    /// applies the after transformer to the result. Uses &self, so original
807    /// transformer remains usable.
808    ///
809    /// # Type Parameters
810    ///
811    /// * `S` - The output type of the after transformer
812    /// * `F` - The type of the after transformer (must implement
813    ///   Transformer<R, S>)
814    ///
815    /// # Parameters
816    ///
817    /// * `after` - The transformer to apply after self. **Note: This parameter
818    ///   is passed by value and will transfer ownership.** If you need to
819    ///   preserve the original transformer, clone it first (if it implements
820    ///   `Clone`). Can be:
821    ///   - A closure: `|x: R| -> S`
822    ///   - A function pointer: `fn(R) -> S`
823    ///   - A `BoxTransformer<R, S>`
824    ///   - An `RcTransformer<R, S>`
825    ///   - An `ArcTransformer<R, S>` (will be moved)
826    ///   - Any type implementing `Transformer<R, S> + Send + Sync`
827    ///
828    /// # Returns
829    ///
830    /// A new ArcTransformer representing the composition
831    ///
832    /// # Examples
833    ///
834    /// ## Direct value passing (ownership transfer)
835    ///
836    /// ```rust
837    /// use prism3_function::{ArcTransformer, Transformer};
838    ///
839    /// let double = ArcTransformer::new(|x: i32| x * 2);
840    /// let to_string = ArcTransformer::new(|x: i32| x.to_string());
841    ///
842    /// // to_string is moved here
843    /// let composed = double.and_then(to_string);
844    ///
845    /// // Original double transformer still usable (uses &self)
846    /// assert_eq!(double.apply(21), 42);
847    /// assert_eq!(composed.apply(21), "42");
848    /// // to_string.apply(5); // Would not compile - moved
849    /// ```
850    ///
851    /// ## Preserving original with clone
852    ///
853    /// ```rust
854    /// use prism3_function::{ArcTransformer, Transformer};
855    ///
856    /// let double = ArcTransformer::new(|x: i32| x * 2);
857    /// let to_string = ArcTransformer::new(|x: i32| x.to_string());
858    ///
859    /// // Clone to preserve original
860    /// let composed = double.and_then(to_string.clone());
861    /// assert_eq!(composed.apply(21), "42");
862    ///
863    /// // Both originals still usable
864    /// assert_eq!(double.apply(21), 42);
865    /// assert_eq!(to_string.apply(5), "5");
866    /// ```
867    pub fn and_then<S, F>(&self, after: F) -> ArcTransformer<T, S>
868    where
869        S: Send + Sync + 'static,
870        F: Transformer<R, S> + Send + Sync + 'static,
871    {
872        let self_fn = self.function.clone();
873        ArcTransformer {
874            function: Arc::new(move |x: T| after.apply(self_fn(x))),
875        }
876    }
877
878    /// Reverse composition - applies before first, then self
879    ///
880    /// Creates a new transformer that applies the before transformer first,
881    /// then applies this transformer to the result. Uses &self, so original
882    /// transformer remains usable.
883    ///
884    /// # Type Parameters
885    ///
886    /// * `S` - The input type of the before transformer
887    /// * `F` - The type of the before transformer (must implement
888    ///   Transformer<S, T>)
889    ///
890    /// # Parameters
891    ///
892    /// * `before` - The transformer to apply before self. **Note: This parameter
893    ///   is passed by value and will transfer ownership.** If you need to
894    ///   preserve the original transformer, clone it first (if it implements
895    ///   `Clone`). Can be:
896    ///   - A closure: `|x: S| -> T`
897    ///   - A function pointer: `fn(S) -> T`
898    ///   - A `BoxTransformer<S, T>`
899    ///   - An `RcTransformer<S, T>`
900    ///   - An `ArcTransformer<S, T>` (will be moved)
901    ///   - Any type implementing `Transformer<S, T> + Send + Sync`
902    ///
903    /// # Returns
904    ///
905    /// A new ArcTransformer representing the composition
906    ///
907    /// # Examples
908    ///
909    /// ## Direct value passing (ownership transfer)
910    ///
911    /// ```rust
912    /// use prism3_function::{ArcTransformer, Transformer};
913    ///
914    /// let double = ArcTransformer::new(|x: i32| x * 2);
915    /// let add_one = ArcTransformer::new(|x: i32| x + 1);
916    ///
917    /// // add_one is moved here
918    /// let composed = double.compose(add_one);
919    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
920    /// // add_one.apply(3); // Would not compile - moved
921    /// ```
922    ///
923    /// ## Preserving original with clone
924    ///
925    /// ```rust
926    /// use prism3_function::{ArcTransformer, Transformer};
927    ///
928    /// let double = ArcTransformer::new(|x: i32| x * 2);
929    /// let add_one = ArcTransformer::new(|x: i32| x + 1);
930    ///
931    /// // Clone to preserve original
932    /// let composed = double.compose(add_one.clone());
933    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
934    ///
935    /// // Both originals still usable
936    /// assert_eq!(double.apply(10), 20);
937    /// assert_eq!(add_one.apply(3), 4);
938    /// ```
939    pub fn compose<S, F>(&self, before: F) -> ArcTransformer<S, R>
940    where
941        S: Send + Sync + 'static,
942        F: Transformer<S, T> + Send + Sync + 'static,
943    {
944        let self_fn = self.function.clone();
945        ArcTransformer {
946            function: Arc::new(move |x: S| self_fn(before.apply(x))),
947        }
948    }
949
950    /// Creates a conditional transformer (thread-safe version)
951    ///
952    /// Returns a transformer that only executes when a predicate is satisfied.
953    /// You must call `or_else()` to provide an alternative transformer.
954    ///
955    /// # Parameters
956    ///
957    /// * `predicate` - The condition to check. **Note: This parameter is passed
958    ///   by value and will transfer ownership.** If you need to preserve the
959    ///   original predicate, clone it first (if it implements `Clone`). Must be
960    ///   `Send + Sync`, can be:
961    ///   - A closure: `|x: &T| -> bool` (requires `Send + Sync`)
962    ///   - A function pointer: `fn(&T) -> bool`
963    ///   - An `ArcPredicate<T>`
964    ///   - Any type implementing `Predicate<T> + Send + Sync`
965    ///
966    /// # Returns
967    ///
968    /// Returns `ArcConditionalTransformer<T, R>`
969    ///
970    /// # Examples
971    ///
972    /// ## Basic usage with or_else
973    ///
974    /// ```rust
975    /// use prism3_function::{Transformer, ArcTransformer};
976    ///
977    /// let double = ArcTransformer::new(|x: i32| x * 2);
978    /// let identity = ArcTransformer::<i32, i32>::identity();
979    /// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
980    ///
981    /// let conditional_clone = conditional.clone();
982    ///
983    /// assert_eq!(conditional.apply(5), 10);
984    /// assert_eq!(conditional_clone.apply(-5), -5);
985    /// ```
986    ///
987    /// ## Preserving predicate with clone
988    ///
989    /// ```rust
990    /// use prism3_function::{Transformer, ArcTransformer, ArcPredicate};
991    ///
992    /// let double = ArcTransformer::new(|x: i32| x * 2);
993    /// let is_positive = ArcPredicate::new(|x: &i32| *x > 0);
994    ///
995    /// // Clone to preserve original predicate
996    /// let conditional = double.when(is_positive.clone())
997    ///     .or_else(ArcTransformer::identity());
998    ///
999    /// assert_eq!(conditional.apply(5), 10);
1000    ///
1001    /// // Original predicate still usable
1002    /// assert!(is_positive.test(&3));
1003    /// ```
1004    pub fn when<P>(&self, predicate: P) -> ArcConditionalTransformer<T, R>
1005    where
1006        P: Predicate<T> + Send + Sync + 'static,
1007    {
1008        ArcConditionalTransformer {
1009            transformer: self.clone(),
1010            predicate: predicate.into_arc(),
1011        }
1012    }
1013}
1014
1015impl<T, R> ArcTransformer<T, R>
1016where
1017    T: Send + Sync + 'static,
1018    R: Clone + 'static,
1019{
1020    /// Creates a constant transformer
1021    ///
1022    /// # Examples
1023    ///
1024    /// ```rust
1025    /// use prism3_function::{ArcTransformer, Transformer};
1026    ///
1027    /// let constant = ArcTransformer::constant("hello");
1028    /// assert_eq!(constant.apply(123), "hello");
1029    /// ```
1030    pub fn constant(value: R) -> ArcTransformer<T, R>
1031    where
1032        R: Send + Sync,
1033    {
1034        ArcTransformer::new(move |_| value.clone())
1035    }
1036}
1037
1038impl<T, R> Transformer<T, R> for ArcTransformer<T, R> {
1039    fn apply(&self, input: T) -> R {
1040        (self.function)(input)
1041    }
1042
1043    fn into_box(self) -> BoxTransformer<T, R>
1044    where
1045        T: 'static,
1046        R: 'static,
1047    {
1048        BoxTransformer::new(move |t| (self.function)(t))
1049    }
1050
1051    fn into_rc(self) -> RcTransformer<T, R>
1052    where
1053        T: 'static,
1054        R: 'static,
1055    {
1056        RcTransformer::new(move |t| (self.function)(t))
1057    }
1058
1059    fn into_arc(self) -> ArcTransformer<T, R>
1060    where
1061        T: Send + Sync + 'static,
1062        R: Send + Sync + 'static,
1063    {
1064        self
1065    }
1066
1067    fn into_fn(self) -> impl Fn(T) -> R
1068    where
1069        T: 'static,
1070        R: 'static,
1071    {
1072        move |t| (self.function)(t)
1073    }
1074
1075    fn to_box(&self) -> BoxTransformer<T, R>
1076    where
1077        T: 'static,
1078        R: 'static,
1079    {
1080        let self_fn = self.function.clone();
1081        BoxTransformer::new(move |t| self_fn(t))
1082    }
1083
1084    fn to_rc(&self) -> RcTransformer<T, R>
1085    where
1086        T: 'static,
1087        R: 'static,
1088    {
1089        let self_fn = self.function.clone();
1090        RcTransformer::new(move |t| self_fn(t))
1091    }
1092
1093    fn to_arc(&self) -> ArcTransformer<T, R>
1094    where
1095        T: Send + Sync + 'static,
1096        R: Send + Sync + 'static,
1097    {
1098        self.clone()
1099    }
1100
1101    fn to_fn(&self) -> impl Fn(T) -> R
1102    where
1103        T: 'static,
1104        R: 'static,
1105    {
1106        let self_fn = self.function.clone();
1107        move |t| self_fn(t)
1108    }
1109}
1110
1111impl<T, R> Clone for ArcTransformer<T, R> {
1112    fn clone(&self) -> Self {
1113        ArcTransformer {
1114            function: Arc::clone(&self.function),
1115        }
1116    }
1117}
1118
1119// ============================================================================
1120// ArcConditionalTransformer - Arc-based Conditional Transformer
1121// ============================================================================
1122
1123/// ArcConditionalTransformer struct
1124///
1125/// A thread-safe conditional transformer that only executes when a predicate is
1126/// satisfied. Uses `ArcTransformer` and `ArcPredicate` for shared ownership
1127/// across threads.
1128///
1129/// This type is typically created by calling `ArcTransformer::when()` and is
1130/// designed to work with the `or_else()` method to create if-then-else logic.
1131///
1132/// # Features
1133///
1134/// - **Shared Ownership**: Cloneable via `Arc`, multiple owners allowed
1135/// - **Thread-Safe**: Implements `Send + Sync`, safe for concurrent use
1136/// - **Conditional Execution**: Only transforms when predicate returns `true`
1137/// - **Chainable**: Can add `or_else` branch to create if-then-else logic
1138///
1139/// # Examples
1140///
1141/// ```rust
1142/// use prism3_function::{Transformer, ArcTransformer};
1143///
1144/// let double = ArcTransformer::new(|x: i32| x * 2);
1145/// let identity = ArcTransformer::<i32, i32>::identity();
1146/// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
1147///
1148/// let conditional_clone = conditional.clone();
1149///
1150/// assert_eq!(conditional.apply(5), 10);
1151/// assert_eq!(conditional_clone.apply(-5), -5);
1152/// ```
1153///
1154/// # Author
1155///
1156/// Haixing Hu
1157pub struct ArcConditionalTransformer<T, R> {
1158    transformer: ArcTransformer<T, R>,
1159    predicate: ArcPredicate<T>,
1160}
1161
1162impl<T, R> ArcConditionalTransformer<T, R>
1163where
1164    T: Send + Sync + 'static,
1165    R: 'static,
1166{
1167    /// Adds an else branch (thread-safe version)
1168    ///
1169    /// Executes the original transformer when the condition is satisfied,
1170    /// otherwise executes else_transformer.
1171    ///
1172    /// # Parameters
1173    ///
1174    /// * `else_transformer` - The transformer for the else branch, can be:
1175    ///   - Closure: `|x: T| -> R` (must be `Send + Sync`)
1176    ///   - `ArcTransformer<T, R>`, `BoxTransformer<T, R>`
1177    ///   - Any type implementing `Transformer<T, R> + Send + Sync`
1178    ///
1179    /// # Returns
1180    ///
1181    /// Returns the composed `ArcTransformer<T, R>`
1182    ///
1183    /// # Examples
1184    ///
1185    /// ## Using a closure (recommended)
1186    ///
1187    /// ```rust
1188    /// use prism3_function::{Transformer, ArcTransformer};
1189    ///
1190    /// let double = ArcTransformer::new(|x: i32| x * 2);
1191    /// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
1192    ///
1193    /// assert_eq!(conditional.apply(5), 10);
1194    /// assert_eq!(conditional.apply(-5), 5);
1195    /// ```
1196    pub fn or_else<F>(self, else_transformer: F) -> ArcTransformer<T, R>
1197    where
1198        F: Transformer<T, R> + Send + Sync + 'static,
1199        R: Send + Sync,
1200    {
1201        let pred = self.predicate;
1202        let then_trans = self.transformer;
1203        ArcTransformer::new(move |t| {
1204            if pred.test(&t) {
1205                then_trans.apply(t)
1206            } else {
1207                else_transformer.apply(t)
1208            }
1209        })
1210    }
1211}
1212
1213impl<T, R> Clone for ArcConditionalTransformer<T, R> {
1214    /// Clones the conditional transformer
1215    ///
1216    /// Creates a new instance that shares the underlying transformer and
1217    /// predicate with the original instance.
1218    fn clone(&self) -> Self {
1219        Self {
1220            transformer: self.transformer.clone(),
1221            predicate: self.predicate.clone(),
1222        }
1223    }
1224}
1225
1226// ============================================================================
1227// RcTransformer - Rc<dyn Fn(T) -> R>
1228// ============================================================================
1229
1230/// RcTransformer - single-threaded transformer wrapper
1231///
1232/// A single-threaded, clonable transformer wrapper optimized for scenarios
1233/// that require sharing without thread-safety overhead.
1234///
1235/// # Features
1236///
1237/// - **Based on**: `Rc<dyn Fn(T) -> R>`
1238/// - **Ownership**: Shared ownership via reference counting (non-atomic)
1239/// - **Reusability**: Can be called multiple times (each call consumes its
1240///   input)
1241/// - **Thread Safety**: Not thread-safe (no `Send + Sync`)
1242/// - **Clonable**: Cheap cloning via `Rc::clone`
1243///
1244/// # Author
1245///
1246/// Hu Haixing
1247pub struct RcTransformer<T, R> {
1248    function: Rc<dyn Fn(T) -> R>,
1249}
1250
1251impl<T, R> RcTransformer<T, R>
1252where
1253    T: 'static,
1254    R: 'static,
1255{
1256    /// Creates a new RcTransformer
1257    ///
1258    /// # Parameters
1259    ///
1260    /// * `f` - The closure or function to wrap
1261    ///
1262    /// # Examples
1263    ///
1264    /// ```rust
1265    /// use prism3_function::{RcTransformer, Transformer};
1266    ///
1267    /// let double = RcTransformer::new(|x: i32| x * 2);
1268    /// assert_eq!(double.apply(21), 42);
1269    /// ```
1270    pub fn new<F>(f: F) -> Self
1271    where
1272        F: Fn(T) -> R + 'static,
1273    {
1274        RcTransformer {
1275            function: Rc::new(f),
1276        }
1277    }
1278
1279    /// Creates an identity transformer
1280    ///
1281    /// # Examples
1282    ///
1283    /// ```rust
1284    /// use prism3_function::{RcTransformer, Transformer};
1285    ///
1286    /// let identity = RcTransformer::<i32, i32>::identity();
1287    /// assert_eq!(identity.apply(42), 42);
1288    /// ```
1289    pub fn identity() -> RcTransformer<T, T> {
1290        RcTransformer::new(|x| x)
1291    }
1292
1293    /// Chain composition - applies self first, then after
1294    ///
1295    /// Creates a new transformer that applies this transformer first, then
1296    /// applies the after transformer to the result. Uses &self, so original
1297    /// transformer remains usable.
1298    ///
1299    /// # Type Parameters
1300    ///
1301    /// * `S` - The output type of the after transformer
1302    /// * `F` - The type of the after transformer (must implement
1303    ///   Transformer<R, S>)
1304    ///
1305    /// # Parameters
1306    ///
1307    /// * `after` - The transformer to apply after self. **Note: This parameter
1308    ///   is passed by value and will transfer ownership.** If you need to
1309    ///   preserve the original transformer, clone it first (if it implements
1310    ///   `Clone`). Can be:
1311    ///   - A closure: `|x: R| -> S`
1312    ///   - A function pointer: `fn(R) -> S`
1313    ///   - A `BoxTransformer<R, S>`
1314    ///   - An `RcTransformer<R, S>` (will be moved)
1315    ///   - An `ArcTransformer<R, S>`
1316    ///   - Any type implementing `Transformer<R, S>`
1317    ///
1318    /// # Returns
1319    ///
1320    /// A new RcTransformer representing the composition
1321    ///
1322    /// # Examples
1323    ///
1324    /// ## Direct value passing (ownership transfer)
1325    ///
1326    /// ```rust
1327    /// use prism3_function::{RcTransformer, Transformer};
1328    ///
1329    /// let double = RcTransformer::new(|x: i32| x * 2);
1330    /// let to_string = RcTransformer::new(|x: i32| x.to_string());
1331    ///
1332    /// // to_string is moved here
1333    /// let composed = double.and_then(to_string);
1334    ///
1335    /// // Original double transformer still usable (uses &self)
1336    /// assert_eq!(double.apply(21), 42);
1337    /// assert_eq!(composed.apply(21), "42");
1338    /// // to_string.apply(5); // Would not compile - moved
1339    /// ```
1340    ///
1341    /// ## Preserving original with clone
1342    ///
1343    /// ```rust
1344    /// use prism3_function::{RcTransformer, Transformer};
1345    ///
1346    /// let double = RcTransformer::new(|x: i32| x * 2);
1347    /// let to_string = RcTransformer::new(|x: i32| x.to_string());
1348    ///
1349    /// // Clone to preserve original
1350    /// let composed = double.and_then(to_string.clone());
1351    /// assert_eq!(composed.apply(21), "42");
1352    ///
1353    /// // Both originals still usable
1354    /// assert_eq!(double.apply(21), 42);
1355    /// assert_eq!(to_string.apply(5), "5");
1356    /// ```
1357    pub fn and_then<S, F>(&self, after: F) -> RcTransformer<T, S>
1358    where
1359        S: 'static,
1360        F: Transformer<R, S> + 'static,
1361    {
1362        let self_fn = self.function.clone();
1363        RcTransformer {
1364            function: Rc::new(move |x: T| after.apply(self_fn(x))),
1365        }
1366    }
1367
1368    /// Reverse composition - applies before first, then self
1369    ///
1370    /// Creates a new transformer that applies the before transformer first,
1371    /// then applies this transformer to the result. Uses &self, so original
1372    /// transformer remains usable.
1373    ///
1374    /// # Type Parameters
1375    ///
1376    /// * `S` - The input type of the before transformer
1377    /// * `F` - The type of the before transformer (must implement
1378    ///   Transformer<S, T>)
1379    ///
1380    /// # Parameters
1381    ///
1382    /// * `before` - The transformer to apply before self. **Note: This parameter
1383    ///   is passed by value and will transfer ownership.** If you need to
1384    ///   preserve the original transformer, clone it first (if it implements
1385    ///   `Clone`). Can be:
1386    ///   - A closure: `|x: S| -> T`
1387    ///   - A function pointer: `fn(S) -> T`
1388    ///   - A `BoxTransformer<S, T>`
1389    ///   - An `RcTransformer<S, T>` (will be moved)
1390    ///   - An `ArcTransformer<S, T>`
1391    ///   - Any type implementing `Transformer<S, T>`
1392    ///
1393    /// # Returns
1394    ///
1395    /// A new RcTransformer representing the composition
1396    ///
1397    /// # Examples
1398    ///
1399    /// ## Direct value passing (ownership transfer)
1400    ///
1401    /// ```rust
1402    /// use prism3_function::{RcTransformer, Transformer};
1403    ///
1404    /// let double = RcTransformer::new(|x: i32| x * 2);
1405    /// let add_one = RcTransformer::new(|x: i32| x + 1);
1406    ///
1407    /// // add_one is moved here
1408    /// let composed = double.compose(add_one);
1409    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
1410    /// // add_one.apply(3); // Would not compile - moved
1411    /// ```
1412    ///
1413    /// ## Preserving original with clone
1414    ///
1415    /// ```rust
1416    /// use prism3_function::{RcTransformer, Transformer};
1417    ///
1418    /// let double = RcTransformer::new(|x: i32| x * 2);
1419    /// let add_one = RcTransformer::new(|x: i32| x + 1);
1420    ///
1421    /// // Clone to preserve original
1422    /// let composed = double.compose(add_one.clone());
1423    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
1424    ///
1425    /// // Both originals still usable
1426    /// assert_eq!(double.apply(10), 20);
1427    /// assert_eq!(add_one.apply(3), 4);
1428    /// ```
1429    pub fn compose<S, F>(&self, before: F) -> RcTransformer<S, R>
1430    where
1431        S: 'static,
1432        F: Transformer<S, T> + 'static,
1433    {
1434        let self_clone = Rc::clone(&self.function);
1435        RcTransformer {
1436            function: Rc::new(move |x: S| self_clone(before.apply(x))),
1437        }
1438    }
1439
1440    /// Creates a conditional transformer (single-threaded shared version)
1441    ///
1442    /// Returns a transformer that only executes when a predicate is satisfied.
1443    /// You must call `or_else()` to provide an alternative transformer.
1444    ///
1445    /// # Parameters
1446    ///
1447    /// * `predicate` - The condition to check. **Note: This parameter is passed
1448    ///   by value and will transfer ownership.** If you need to preserve the
1449    ///   original predicate, clone it first (if it implements `Clone`). Can be:
1450    ///   - A closure: `|x: &T| -> bool`
1451    ///   - A function pointer: `fn(&T) -> bool`
1452    ///   - A `BoxPredicate<T>`
1453    ///   - An `RcPredicate<T>`
1454    ///   - An `ArcPredicate<T>`
1455    ///   - Any type implementing `Predicate<T>`
1456    ///
1457    /// # Returns
1458    ///
1459    /// Returns `RcConditionalTransformer<T, R>`
1460    ///
1461    /// # Examples
1462    ///
1463    /// ## Basic usage with or_else
1464    ///
1465    /// ```rust
1466    /// use prism3_function::{Transformer, RcTransformer};
1467    ///
1468    /// let double = RcTransformer::new(|x: i32| x * 2);
1469    /// let identity = RcTransformer::<i32, i32>::identity();
1470    /// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
1471    ///
1472    /// let conditional_clone = conditional.clone();
1473    ///
1474    /// assert_eq!(conditional.apply(5), 10);
1475    /// assert_eq!(conditional_clone.apply(-5), -5);
1476    /// ```
1477    ///
1478    /// ## Preserving predicate with clone
1479    ///
1480    /// ```rust
1481    /// use prism3_function::{Transformer, RcTransformer, RcPredicate};
1482    ///
1483    /// let double = RcTransformer::new(|x: i32| x * 2);
1484    /// let is_positive = RcPredicate::new(|x: &i32| *x > 0);
1485    ///
1486    /// // Clone to preserve original predicate
1487    /// let conditional = double.when(is_positive.clone())
1488    ///     .or_else(RcTransformer::identity());
1489    ///
1490    /// assert_eq!(conditional.apply(5), 10);
1491    ///
1492    /// // Original predicate still usable
1493    /// assert!(is_positive.test(&3));
1494    /// ```
1495    pub fn when<P>(&self, predicate: P) -> RcConditionalTransformer<T, R>
1496    where
1497        P: Predicate<T> + 'static,
1498    {
1499        RcConditionalTransformer {
1500            transformer: self.clone(),
1501            predicate: predicate.into_rc(),
1502        }
1503    }
1504}
1505
1506impl<T, R> RcTransformer<T, R>
1507where
1508    T: 'static,
1509    R: Clone + 'static,
1510{
1511    /// Creates a constant transformer
1512    ///
1513    /// # Examples
1514    ///
1515    /// ```rust
1516    /// use prism3_function::{RcTransformer, Transformer};
1517    ///
1518    /// let constant = RcTransformer::constant("hello");
1519    /// assert_eq!(constant.apply(123), "hello");
1520    /// ```
1521    pub fn constant(value: R) -> RcTransformer<T, R> {
1522        RcTransformer::new(move |_| value.clone())
1523    }
1524}
1525
1526impl<T, R> Transformer<T, R> for RcTransformer<T, R> {
1527    fn apply(&self, input: T) -> R {
1528        (self.function)(input)
1529    }
1530
1531    // RcTransformer::into_box() is implemented by the default implementation
1532    // of Transformer::into_box()
1533
1534    fn into_box(self) -> BoxTransformer<T, R>
1535    where
1536        T: 'static,
1537        R: 'static,
1538    {
1539        BoxTransformer::new(move |t| (self.function)(t))
1540    }
1541
1542    // Override with zero-cost implementation: directly return itself
1543    fn into_rc(self) -> RcTransformer<T, R>
1544    where
1545        T: 'static,
1546        R: 'static,
1547    {
1548        self
1549    }
1550
1551    // do NOT override RcTransformer::into_arc() because RcTransformer is not Send + Sync
1552    // and calling RcTransformer::into_arc() will cause a compile error
1553
1554    // Override with optimized implementation: wrap the Rc in a
1555    // closure to avoid double indirection
1556    fn into_fn(self) -> impl Fn(T) -> R
1557    where
1558        T: 'static,
1559        R: 'static,
1560    {
1561        move |t| (self.function)(t)
1562    }
1563
1564    // Override with optimized implementation: clone the Rc (cheap)
1565    fn to_box(&self) -> BoxTransformer<T, R>
1566    where
1567        T: 'static,
1568        R: 'static,
1569    {
1570        let self_fn = self.function.clone();
1571        BoxTransformer::new(move |t| self_fn(t))
1572    }
1573
1574    // Override with zero-cost implementation: clone itself
1575    fn to_rc(&self) -> RcTransformer<T, R>
1576    where
1577        T: 'static,
1578        R: 'static,
1579    {
1580        self.clone()
1581    }
1582
1583    // do NOT override RcTransformer::to_arc() because RcTransformer is not Send + Sync
1584    // and calling RcTransformer::to_arc() will cause a compile error
1585
1586    // Override with optimized implementation: clone the Rc (cheap)
1587    fn to_fn(&self) -> impl Fn(T) -> R
1588    where
1589        T: 'static,
1590        R: 'static,
1591    {
1592        let self_fn = self.function.clone();
1593        move |t| self_fn(t)
1594    }
1595}
1596
1597impl<T, R> Clone for RcTransformer<T, R> {
1598    fn clone(&self) -> Self {
1599        RcTransformer {
1600            function: Rc::clone(&self.function),
1601        }
1602    }
1603}
1604
1605// ============================================================================
1606// RcConditionalTransformer - Rc-based Conditional Transformer
1607// ============================================================================
1608
1609/// RcConditionalTransformer struct
1610///
1611/// A single-threaded conditional transformer that only executes when a
1612/// predicate is satisfied. Uses `RcTransformer` and `RcPredicate` for shared
1613/// ownership within a single thread.
1614///
1615/// This type is typically created by calling `RcTransformer::when()` and is
1616/// designed to work with the `or_else()` method to create if-then-else logic.
1617///
1618/// # Features
1619///
1620/// - **Shared Ownership**: Cloneable via `Rc`, multiple owners allowed
1621/// - **Single-Threaded**: Not thread-safe, cannot be sent across threads
1622/// - **Conditional Execution**: Only transforms when predicate returns `true`
1623/// - **No Lock Overhead**: More efficient than `ArcConditionalTransformer`
1624///
1625/// # Examples
1626///
1627/// ```rust
1628/// use prism3_function::{Transformer, RcTransformer};
1629///
1630/// let double = RcTransformer::new(|x: i32| x * 2);
1631/// let identity = RcTransformer::<i32, i32>::identity();
1632/// let conditional = double.when(|x: &i32| *x > 0).or_else(identity);
1633///
1634/// let conditional_clone = conditional.clone();
1635///
1636/// assert_eq!(conditional.apply(5), 10);
1637/// assert_eq!(conditional_clone.apply(-5), -5);
1638/// ```
1639///
1640/// # Author
1641///
1642/// Haixing Hu
1643pub struct RcConditionalTransformer<T, R> {
1644    transformer: RcTransformer<T, R>,
1645    predicate: RcPredicate<T>,
1646}
1647
1648impl<T, R> RcConditionalTransformer<T, R>
1649where
1650    T: 'static,
1651    R: 'static,
1652{
1653    /// Adds an else branch (single-threaded shared version)
1654    ///
1655    /// Executes the original transformer when the condition is satisfied,
1656    /// otherwise executes else_transformer.
1657    ///
1658    /// # Parameters
1659    ///
1660    /// * `else_transformer` - The transformer for the else branch, can be:
1661    ///   - Closure: `|x: T| -> R`
1662    ///   - `RcTransformer<T, R>`, `BoxTransformer<T, R>`
1663    ///   - Any type implementing `Transformer<T, R>`
1664    ///
1665    /// # Returns
1666    ///
1667    /// Returns the composed `RcTransformer<T, R>`
1668    ///
1669    /// # Examples
1670    ///
1671    /// ## Using a closure (recommended)
1672    ///
1673    /// ```rust
1674    /// use prism3_function::{Transformer, RcTransformer};
1675    ///
1676    /// let double = RcTransformer::new(|x: i32| x * 2);
1677    /// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
1678    ///
1679    /// assert_eq!(conditional.apply(5), 10);
1680    /// assert_eq!(conditional.apply(-5), 5);
1681    /// ```
1682    pub fn or_else<F>(self, else_transformer: F) -> RcTransformer<T, R>
1683    where
1684        F: Transformer<T, R> + 'static,
1685    {
1686        let pred = self.predicate;
1687        let then_trans = self.transformer;
1688        RcTransformer::new(move |t| {
1689            if pred.test(&t) {
1690                then_trans.apply(t)
1691            } else {
1692                else_transformer.apply(t)
1693            }
1694        })
1695    }
1696}
1697
1698impl<T, R> Clone for RcConditionalTransformer<T, R> {
1699    /// Clones the conditional transformer
1700    ///
1701    /// Creates a new instance that shares the underlying transformer and
1702    /// predicate with the original instance.
1703    fn clone(&self) -> Self {
1704        Self {
1705            transformer: self.transformer.clone(),
1706            predicate: self.predicate.clone(),
1707        }
1708    }
1709}
1710
1711// ============================================================================
1712// Blanket implementation for standard Fn trait
1713// ============================================================================
1714
1715/// Implement Transformer<T, R> for any type that implements Fn(T) -> R
1716///
1717/// This allows closures and function pointers to be used directly with our
1718/// Transformer trait without wrapping.
1719///
1720/// # Examples
1721///
1722/// ```rust
1723/// use prism3_function::Transformer;
1724///
1725/// fn double(x: i32) -> i32 { x * 2 }
1726///
1727/// assert_eq!(double.apply(21), 42);
1728///
1729/// let triple = |x: i32| x * 3;
1730/// assert_eq!(triple.apply(14), 42);
1731/// ```
1732///
1733/// # Author
1734///
1735/// Hu Haixing
1736impl<F, T, R> Transformer<T, R> for F
1737where
1738    F: Fn(T) -> R,
1739    T: 'static,
1740    R: 'static,
1741{
1742    fn apply(&self, input: T) -> R {
1743        self(input)
1744    }
1745
1746    fn into_box(self) -> BoxTransformer<T, R>
1747    where
1748        Self: Sized + 'static,
1749    {
1750        BoxTransformer::new(self)
1751    }
1752
1753    fn into_rc(self) -> RcTransformer<T, R>
1754    where
1755        Self: Sized + 'static,
1756    {
1757        RcTransformer::new(self)
1758    }
1759
1760    fn into_arc(self) -> ArcTransformer<T, R>
1761    where
1762        Self: Sized + Send + Sync + 'static,
1763        T: Send + Sync + 'static,
1764        R: Send + Sync + 'static,
1765    {
1766        ArcTransformer::new(self)
1767    }
1768
1769    fn into_fn(self) -> impl Fn(T) -> R
1770    where
1771        Self: Sized + 'static,
1772    {
1773        self
1774    }
1775
1776    fn to_box(&self) -> BoxTransformer<T, R>
1777    where
1778        Self: Clone + Sized + 'static,
1779    {
1780        self.clone().into_box()
1781    }
1782
1783    fn to_rc(&self) -> RcTransformer<T, R>
1784    where
1785        Self: Clone + Sized + 'static,
1786    {
1787        self.clone().into_rc()
1788    }
1789
1790    fn to_arc(&self) -> ArcTransformer<T, R>
1791    where
1792        Self: Clone + Sized + Send + Sync + 'static,
1793        T: Send + Sync + 'static,
1794        R: Send + Sync + 'static,
1795    {
1796        self.clone().into_arc()
1797    }
1798
1799    fn to_fn(&self) -> impl Fn(T) -> R
1800    where
1801        Self: Clone + Sized + 'static,
1802    {
1803        self.clone()
1804    }
1805}
1806
1807// ============================================================================
1808// FnTransformerOps - Extension trait for closure transformers
1809// ============================================================================
1810
1811/// Extension trait for closures implementing `Fn(T) -> R`
1812///
1813/// Provides composition methods (`and_then`, `compose`, `when`) for closures
1814/// and function pointers without requiring explicit wrapping in
1815/// `BoxTransformer`, `RcTransformer`, or `ArcTransformer`.
1816///
1817/// This trait is automatically implemented for all closures and function
1818/// pointers that implement `Fn(T) -> R`.
1819///
1820/// # Design Rationale
1821///
1822/// While closures automatically implement `Transformer<T, R>` through blanket
1823/// implementation, they don't have access to instance methods like `and_then`,
1824/// `compose`, and `when`. This extension trait provides those methods,
1825/// returning `BoxTransformer` for maximum flexibility.
1826///
1827/// # Examples
1828///
1829/// ## Chain composition with and_then
1830///
1831/// ```rust
1832/// use prism3_function::{Transformer, FnTransformerOps};
1833///
1834/// let double = |x: i32| x * 2;
1835/// let to_string = |x: i32| x.to_string();
1836///
1837/// let composed = double.and_then(to_string);
1838/// assert_eq!(composed.apply(21), "42");
1839/// ```
1840///
1841/// ## Reverse composition with compose
1842///
1843/// ```rust
1844/// use prism3_function::{Transformer, FnTransformerOps};
1845///
1846/// let double = |x: i32| x * 2;
1847/// let add_one = |x: i32| x + 1;
1848///
1849/// let composed = double.compose(add_one);
1850/// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
1851/// ```
1852///
1853/// ## Conditional transformation with when
1854///
1855/// ```rust
1856/// use prism3_function::{Transformer, FnTransformerOps};
1857///
1858/// let double = |x: i32| x * 2;
1859/// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
1860///
1861/// assert_eq!(conditional.apply(5), 10);
1862/// assert_eq!(conditional.apply(-5), 5);
1863/// ```
1864///
1865/// # Author
1866///
1867/// Hu Haixing
1868pub trait FnTransformerOps<T, R>: Fn(T) -> R + Sized + 'static {
1869    /// Chain composition - applies self first, then after
1870    ///
1871    /// Creates a new transformer that applies this transformer first, then
1872    /// applies the after transformer to the result. Consumes self and returns
1873    /// a `BoxTransformer`.
1874    ///
1875    /// # Type Parameters
1876    ///
1877    /// * `S` - The output type of the after transformer
1878    /// * `F` - The type of the after transformer (must implement Transformer<R, S>)
1879    ///
1880    /// # Parameters
1881    ///
1882    /// * `after` - The transformer to apply after self. **Note: This parameter
1883    ///   is passed by value and will transfer ownership.** If you need to
1884    ///   preserve the original transformer, clone it first (if it implements
1885    ///   `Clone`). Can be:
1886    ///   - A closure: `|x: R| -> S`
1887    ///   - A function pointer: `fn(R) -> S`
1888    ///   - A `BoxTransformer<R, S>`
1889    ///   - An `RcTransformer<R, S>`
1890    ///   - An `ArcTransformer<R, S>`
1891    ///   - Any type implementing `Transformer<R, S>`
1892    ///
1893    /// # Returns
1894    ///
1895    /// A new `BoxTransformer<T, S>` representing the composition
1896    ///
1897    /// # Examples
1898    ///
1899    /// ## Direct value passing (ownership transfer)
1900    ///
1901    /// ```rust
1902    /// use prism3_function::{Transformer, FnTransformerOps, BoxTransformer};
1903    ///
1904    /// let double = |x: i32| x * 2;
1905    /// let to_string = BoxTransformer::new(|x: i32| x.to_string());
1906    ///
1907    /// // to_string is moved here
1908    /// let composed = double.and_then(to_string);
1909    /// assert_eq!(composed.apply(21), "42");
1910    /// // to_string.apply(5); // Would not compile - moved
1911    /// ```
1912    ///
1913    /// ## Preserving original with clone
1914    ///
1915    /// ```rust
1916    /// use prism3_function::{Transformer, FnTransformerOps, BoxTransformer};
1917    ///
1918    /// let double = |x: i32| x * 2;
1919    /// let to_string = BoxTransformer::new(|x: i32| x.to_string());
1920    ///
1921    /// // Clone to preserve original
1922    /// let composed = double.and_then(to_string.clone());
1923    /// assert_eq!(composed.apply(21), "42");
1924    ///
1925    /// // Original still usable
1926    /// assert_eq!(to_string.apply(5), "5");
1927    /// ```
1928    fn and_then<S, F>(self, after: F) -> BoxTransformer<T, S>
1929    where
1930        S: 'static,
1931        F: Transformer<R, S> + 'static,
1932        T: 'static,
1933        R: 'static,
1934    {
1935        BoxTransformer::new(move |x: T| after.apply(self(x)))
1936    }
1937
1938    /// Reverse composition - applies before first, then self
1939    ///
1940    /// Creates a new transformer that applies the before transformer first,
1941    /// then applies this transformer to the result. Consumes self and returns
1942    /// a `BoxTransformer`.
1943    ///
1944    /// # Type Parameters
1945    ///
1946    /// * `S` - The input type of the before transformer
1947    /// * `F` - The type of the before transformer (must implement Transformer<S, T>)
1948    ///
1949    /// # Parameters
1950    ///
1951    /// * `before` - The transformer to apply before self. **Note: This parameter
1952    ///   is passed by value and will transfer ownership.** If you need to
1953    ///   preserve the original transformer, clone it first (if it implements
1954    ///   `Clone`). Can be:
1955    ///   - A closure: `|x: S| -> T`
1956    ///   - A function pointer: `fn(S) -> T`
1957    ///   - A `BoxTransformer<S, T>`
1958    ///   - An `RcTransformer<S, T>`
1959    ///   - An `ArcTransformer<S, T>`
1960    ///   - Any type implementing `Transformer<S, T>`
1961    ///
1962    /// # Returns
1963    ///
1964    /// A new `BoxTransformer<S, R>` representing the composition
1965    ///
1966    /// # Examples
1967    ///
1968    /// ## Direct value passing (ownership transfer)
1969    ///
1970    /// ```rust
1971    /// use prism3_function::{Transformer, FnTransformerOps, BoxTransformer};
1972    ///
1973    /// let double = |x: i32| x * 2;
1974    /// let add_one = BoxTransformer::new(|x: i32| x + 1);
1975    ///
1976    /// // add_one is moved here
1977    /// let composed = double.compose(add_one);
1978    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
1979    /// // add_one.apply(3); // Would not compile - moved
1980    /// ```
1981    ///
1982    /// ## Preserving original with clone
1983    ///
1984    /// ```rust
1985    /// use prism3_function::{Transformer, FnTransformerOps, BoxTransformer};
1986    ///
1987    /// let double = |x: i32| x * 2;
1988    /// let add_one = BoxTransformer::new(|x: i32| x + 1);
1989    ///
1990    /// // Clone to preserve original
1991    /// let composed = double.compose(add_one.clone());
1992    /// assert_eq!(composed.apply(5), 12); // (5 + 1) * 2
1993    ///
1994    /// // Original still usable
1995    /// assert_eq!(add_one.apply(3), 4);
1996    /// ```
1997    fn compose<S, F>(self, before: F) -> BoxTransformer<S, R>
1998    where
1999        S: 'static,
2000        F: Transformer<S, T> + 'static,
2001        T: 'static,
2002        R: 'static,
2003    {
2004        BoxTransformer::new(move |x: S| self(before.apply(x)))
2005    }
2006
2007    /// Creates a conditional transformer
2008    ///
2009    /// Returns a transformer that only executes when a predicate is satisfied.
2010    /// You must call `or_else()` to provide an alternative transformer for when
2011    /// the condition is not satisfied.
2012    ///
2013    /// # Parameters
2014    ///
2015    /// * `predicate` - The condition to check. **Note: This parameter is passed
2016    ///   by value and will transfer ownership.** If you need to preserve the
2017    ///   original predicate, clone it first (if it implements `Clone`). Can be:
2018    ///   - A closure: `|x: &T| -> bool`
2019    ///   - A function pointer: `fn(&T) -> bool`
2020    ///   - A `BoxPredicate<T>`
2021    ///   - An `RcPredicate<T>`
2022    ///   - An `ArcPredicate<T>`
2023    ///   - Any type implementing `Predicate<T>`
2024    ///
2025    /// # Returns
2026    ///
2027    /// Returns `BoxConditionalTransformer<T, R>`
2028    ///
2029    /// # Examples
2030    ///
2031    /// ## Basic usage with or_else
2032    ///
2033    /// ```rust
2034    /// use prism3_function::{Transformer, FnTransformerOps};
2035    ///
2036    /// let double = |x: i32| x * 2;
2037    /// let conditional = double.when(|x: &i32| *x > 0).or_else(|x: i32| -x);
2038    ///
2039    /// assert_eq!(conditional.apply(5), 10);
2040    /// assert_eq!(conditional.apply(-5), 5);
2041    /// ```
2042    ///
2043    /// ## Preserving predicate with clone
2044    ///
2045    /// ```rust
2046    /// use prism3_function::{Transformer, FnTransformerOps, BoxPredicate};
2047    ///
2048    /// let double = |x: i32| x * 2;
2049    /// let is_positive = BoxPredicate::new(|x: &i32| *x > 0);
2050    ///
2051    /// // Clone to preserve original predicate
2052    /// let conditional = double.when(is_positive.clone())
2053    ///     .or_else(|x: i32| -x);
2054    ///
2055    /// assert_eq!(conditional.apply(5), 10);
2056    ///
2057    /// // Original predicate still usable
2058    /// assert!(is_positive.test(&3));
2059    /// ```
2060    fn when<P>(self, predicate: P) -> BoxConditionalTransformer<T, R>
2061    where
2062        P: Predicate<T> + 'static,
2063        T: 'static,
2064        R: 'static,
2065    {
2066        BoxTransformer::new(self).when(predicate)
2067    }
2068}
2069
2070/// Blanket implementation of FnTransformerOps for all closures
2071///
2072/// Automatically implements `FnTransformerOps<T, R>` for any type that
2073/// implements `Fn(T) -> R`.
2074///
2075/// # Author
2076///
2077/// Hu Haixing
2078impl<T, R, F> FnTransformerOps<T, R> for F where F: Fn(T) -> R + 'static {}
2079
2080// ============================================================================
2081// UnaryOperator Trait - Marker trait for Transformer<T, T>
2082// ============================================================================
2083
2084/// UnaryOperator trait - marker trait for unary operators
2085///
2086/// A unary operator transforms a value of type `T` to another value of the
2087/// same type `T`. This trait extends `Transformer<T, T>` to provide semantic
2088/// clarity for same-type transformations. Equivalent to Java's `UnaryOperator<T>`
2089/// which extends `Function<T, T>`.
2090///
2091/// # Automatic Implementation
2092///
2093/// This trait is automatically implemented for all types that implement
2094/// `Transformer<T, T>`, so you don't need to implement it manually.
2095///
2096/// # Type Parameters
2097///
2098/// * `T` - The type of both input and output values
2099///
2100/// # Examples
2101///
2102/// ## Using in generic constraints
2103///
2104/// ```rust
2105/// use prism3_function::{UnaryOperator, Transformer};
2106///
2107/// fn apply_twice<T, O>(value: T, op: O) -> T
2108/// where
2109///     O: UnaryOperator<T>,
2110///     T: Clone,
2111/// {
2112///     let result = op.apply(value.clone());
2113///     op.apply(result)
2114/// }
2115///
2116/// let increment = |x: i32| x + 1;
2117/// assert_eq!(apply_twice(5, increment), 7); // (5 + 1) + 1
2118/// ```
2119///
2120/// ## With concrete types
2121///
2122/// ```rust
2123/// use prism3_function::{BoxUnaryOperator, UnaryOperator, Transformer};
2124///
2125/// fn create_incrementer() -> BoxUnaryOperator<i32> {
2126///     BoxUnaryOperator::new(|x| x + 1)
2127/// }
2128///
2129/// let op = create_incrementer();
2130/// assert_eq!(op.apply(41), 42);
2131/// ```
2132///
2133/// # Author
2134///
2135/// Hu Haixing
2136pub trait UnaryOperator<T>: Transformer<T, T> {}
2137
2138/// Blanket implementation of UnaryOperator for all Transformer<T, T>
2139///
2140/// This automatically implements `UnaryOperator<T>` for any type that
2141/// implements `Transformer<T, T>`.
2142///
2143/// # Author
2144///
2145/// Hu Haixing
2146impl<F, T> UnaryOperator<T> for F
2147where
2148    F: Transformer<T, T>,
2149    T: 'static,
2150{
2151    // empty
2152}
2153
2154// ============================================================================
2155// Type Aliases for UnaryOperator (Transformer<T, T>)
2156// ============================================================================
2157
2158/// Type alias for `BoxTransformer<T, T>`
2159///
2160/// Represents a unary operator that transforms a value of type `T` to another
2161/// value of the same type `T`, with single ownership semantics. Equivalent to
2162/// Java's `UnaryOperator<T>`.
2163///
2164/// # Examples
2165///
2166/// ```rust
2167/// use prism3_function::{BoxUnaryOperator, Transformer};
2168///
2169/// let increment: BoxUnaryOperator<i32> = BoxUnaryOperator::new(|x| x + 1);
2170/// assert_eq!(increment.apply(41), 42);
2171/// ```
2172///
2173/// # Author
2174///
2175/// Hu Haixing
2176pub type BoxUnaryOperator<T> = BoxTransformer<T, T>;
2177
2178/// Type alias for `ArcTransformer<T, T>`
2179///
2180/// Represents a thread-safe unary operator that transforms a value of type `T`
2181/// to another value of the same type `T`. Equivalent to Java's `UnaryOperator<T>`
2182/// with shared, thread-safe ownership.
2183///
2184/// # Examples
2185///
2186/// ```rust
2187/// use prism3_function::{ArcUnaryOperator, Transformer};
2188///
2189/// let double: ArcUnaryOperator<i32> = ArcUnaryOperator::new(|x| x * 2);
2190/// let double_clone = double.clone();
2191/// assert_eq!(double.apply(21), 42);
2192/// assert_eq!(double_clone.apply(21), 42);
2193/// ```
2194///
2195/// # Author
2196///
2197/// Hu Haixing
2198pub type ArcUnaryOperator<T> = ArcTransformer<T, T>;
2199
2200/// Type alias for `RcTransformer<T, T>`
2201///
2202/// Represents a single-threaded unary operator that transforms a value of type
2203/// `T` to another value of the same type `T`. Equivalent to Java's
2204/// `UnaryOperator<T>` with shared, single-threaded ownership.
2205///
2206/// # Examples
2207///
2208/// ```rust
2209/// use prism3_function::{RcUnaryOperator, Transformer};
2210///
2211/// let negate: RcUnaryOperator<i32> = RcUnaryOperator::new(|x: i32| -x);
2212/// let negate_clone = negate.clone();
2213/// assert_eq!(negate.apply(42), -42);
2214/// assert_eq!(negate_clone.apply(42), -42);
2215/// ```
2216///
2217/// # Author
2218///
2219/// Hu Haixing
2220pub type RcUnaryOperator<T> = RcTransformer<T, T>;