Skip to main content

FunctionCategory

Struct FunctionCategory 

Source
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

These methods provide additional functionality beyond the basic Category and Arrow traits, following category theory principles while offering practical composition utilities.

Source

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.
Source

pub fn when<A, P, F>(predicate: P, transform: F) -> FunctionMorphism<A, A>
where A: 'static, P: Fn(&A) -> bool + 'static, F: Fn(A) -> A + 'static,

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.
Source

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.
Source

pub fn then_if<A, P>( first: &FunctionMorphism<A, A>, second: &FunctionMorphism<A, A>, predicate: P, ) -> FunctionMorphism<A, A>
where A: 'static, P: Fn(&A) -> bool + 'static,

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.
Source

pub fn compose_when<A, P>( first: &FunctionMorphism<A, A>, second: &FunctionMorphism<A, A>, predicate: P, ) -> FunctionMorphism<A, A>
where A: 'static, P: Fn(&A) -> bool + 'static,

👎Deprecated:

Please use then_if instead. Its name more accurately describes the conditional execution flow.

deprecated: rename to then_if Composes two morphisms conditionally based on a predicate.

This applies the first morphism, then conditionally applies the second based on the predicate result.

§See also
  • then_if - The modern replacement for this method.
Source

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

Trait Implementations§

Source§

impl Arrow for FunctionCategory

Source§

fn arrow<B, C, F>(f: F) -> Self::Morphism<B, C>
where F: Fn(B) -> C + 'static,

Lifts a pure function into an arrow. Read more
Source§

fn first<B, C, D>(f: &Self::Morphism<B, C>) -> Self::Morphism<(B, D), (C, D)>
where B: 'static, C: 'static, D: 'static,

Processes the first component of a pair, leaving the second unchanged. Read more
Source§

fn second<B, C, D>(f: &Self::Morphism<B, C>) -> Self::Morphism<(D, B), (D, C)>
where B: 'static, C: 'static, D: 'static,

Processes the second component of a pair, leaving the first unchanged. Read more
Source§

fn split<B, C, D>( f: &Self::Morphism<B, C>, g: &Self::Morphism<B, D>, ) -> Self::Morphism<B, (C, D)>
where B: 'static + Clone, C: 'static, D: 'static,

Splits a computation into two parallel paths. Read more
Source§

fn combine_morphisms<B, C, D, E>( f: &Self::Morphism<B, C>, g: &Self::Morphism<D, E>, ) -> Self::Morphism<(B, D), (C, E)>
where B: 'static, C: 'static, D: 'static, E: 'static,

Combines two arrows to process pairs in parallel. Read more
Source§

impl Category for FunctionCategory

Source§

type Morphism<A, B> = Arc<dyn Fn(A) -> B>

Source§

fn identity_morphism<A>() -> Self::Morphism<A, A>

Creates an identity morphism for the given types. Read more
Source§

fn compose_morphisms<A: 'static, B: 'static, C: 'static>( g: &Self::Morphism<B, C>, f: &Self::Morphism<A, B>, ) -> Self::Morphism<A, C>

Composes two morphisms in the category. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.