pub struct FunctionCategory;Expand description
A concrete implementation of the Category and Arrow traits for functions.
This zero-sized type serves as a namespace for function category operations. All methods are implemented as associated functions on the traits.
Implementations§
Source§impl FunctionCategory
Convenience implementations for FunctionCategory
impl FunctionCategory
Convenience implementations for FunctionCategory
These methods provide additional functionality beyond the basic Category and Arrow traits, following category theory principles while offering practical composition utilities.
Sourcepub fn both<A, B, F>(f: F) -> PairMorphism<A, B>where
A: 'static,
F: Fn(A) -> B + 'static,
pub fn both<A, B, F>(f: F) -> PairMorphism<A, B>where
A: 'static,
F: Fn(A) -> B + 'static,
Creates a morphism that applies a function to both components of a pair.
This is useful when you want to apply the same transformation to both elements of a tuple.
§Examples
use rustica::category::function_category::FunctionCategory;
let double_both = FunctionCategory::both(|x: i32| x * 2);
assert_eq!(double_both((3, 5)), (6, 10));§See also
split- Splitting a single input to two morphisms.combine_morphisms- Combining two different morphisms for a pair input.
Sourcepub fn when<A, P, F>(predicate: P, transform: F) -> FunctionMorphism<A, A>
pub fn when<A, P, F>(predicate: P, transform: F) -> FunctionMorphism<A, A>
Creates a morphism that applies a function only if a predicate is true.
If the predicate is false, the original value is returned unchanged. This is a category-theoretic conditional morphism.
§Examples
use rustica::category::function_category::FunctionCategory;
let double_if_even = FunctionCategory::when(
|x: &i32| x % 2 == 0,
|x: i32| x * 2
);
assert_eq!(double_if_even(4), 8); // Even, so doubled
assert_eq!(double_if_even(3), 3); // Odd, so unchanged§See also
then_if- For conditional composition of two existing morphisms.
Sourcepub fn lift<A, B, F>(f: F) -> FunctionMorphism<A, B>where
F: Fn(A) -> B + 'static,
A: 'static,
B: 'static,
pub fn lift<A, B, F>(f: F) -> FunctionMorphism<A, B>where
F: Fn(A) -> B + 'static,
A: 'static,
B: 'static,
Creates a morphism that lifts a regular function into the category.
This is an alias for the Arrow::arrow method, provided for consistency with the deprecated Composable trait.
§See also
Arrow::arrow- The standard way to lift functions into the category.
Sourcepub fn then_if<A, P>(
first: &FunctionMorphism<A, A>,
second: &FunctionMorphism<A, A>,
predicate: P,
) -> FunctionMorphism<A, A>
pub fn then_if<A, P>( first: &FunctionMorphism<A, A>, second: &FunctionMorphism<A, A>, predicate: P, ) -> FunctionMorphism<A, A>
Conditionally composes two morphisms based on a predicate.
Applies the first morphism, then conditionally applies the second morphism if the predicate evaluates to true on the intermediate result.
§Mathematical Definition
then_if(f, g, p) = λx. let y = f(x) in if p(y) then g(y) else y§Examples
use rustica::category::function_category::FunctionCategory;
use rustica::traits::arrow::Arrow;
let add_one = FunctionCategory::arrow(|x: i32| x + 1);
let double = FunctionCategory::arrow(|x: i32| x * 2);
let is_even = |x: &i32| x % 2 == 0;
let conditional = FunctionCategory::then_if(&add_one, &double, is_even);
assert_eq!(conditional(1), 4); // (1 + 1) * 2 = 4 (2 is even)
assert_eq!(conditional(2), 3); // (2 + 1) = 3 (3 is odd)§See also
when- For lifting a single function with a predicate.
Sourcepub fn compose_when<A, P>(
first: &FunctionMorphism<A, A>,
second: &FunctionMorphism<A, A>,
predicate: P,
) -> FunctionMorphism<A, A>
👎Deprecated: Please use then_if instead. Its name more accurately describes the conditional execution flow.
pub fn compose_when<A, P>( first: &FunctionMorphism<A, A>, second: &FunctionMorphism<A, A>, predicate: P, ) -> FunctionMorphism<A, A>
Please use then_if instead. Its name more accurately describes the conditional execution flow.
Sourcepub fn sequence<A, F>(functions: Vec<F>) -> FunctionMorphism<A, A>where
A: 'static,
F: Fn(A) -> A + 'static,
pub fn sequence<A, F>(functions: Vec<F>) -> FunctionMorphism<A, A>where
A: 'static,
F: Fn(A) -> A + 'static,
Creates a morphism that applies multiple transformations in sequence.
when the functions don’t need to be reused.
If the vector is empty, the resulting morphism is the identity function.
§Examples
use rustica::category::function_category::FunctionCategory;
let pipeline = FunctionCategory::sequence(vec![
|x: i32| x + 1,
|x: i32| x * 2,
|x: i32| x - 3,
]);
assert_eq!(pipeline(5), 9); // ((5 + 1) * 2) - 3 = 9