clm-kernel 0.0.24

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
//! Pure function combinators for the Set category (INV-599): identity, compose, pipe, first_match.
//!
//! `compose(g, f)(x) == g(f(x))` (right to left) and `pipe(f, g)(x) == g(f(x))` (left to right).
//! Both are associative with `identity` as unit.
//!
//! `first_match(rules, x)` searches an ordered rule table (INV-626). A rule declines with `None` and
//! answers with `Some(value)`, so a rule whose value type is itself an `Option` can answer `Some(None)`.

/// `id_A(x) = x`.
pub fn identity<T>(value: T) -> T {
    value
}

/// Right-to-left composition: `compose(g, f)(x) == g(f(x))`.
pub fn compose<A, B, C>(g: impl Fn(B) -> C, f: impl Fn(A) -> B) -> impl Fn(A) -> C {
    move |value| g(f(value))
}

/// Left-to-right composition: `pipe(f, g)(x) == g(f(x))`.
pub fn pipe<A, B, C>(f: impl Fn(A) -> B, g: impl Fn(B) -> C) -> impl Fn(A) -> C {
    move |value| g(f(value))
}

/// The answer of the first rule in table order that matches `value`, or `None` when none does.
pub fn first_match<'r, I: ?Sized, O, R>(rules: impl IntoIterator<Item = &'r R>, value: &I) -> Option<O>
where
    R: Fn(&I) -> Option<O> + ?Sized + 'r,
{
    rules.into_iter().find_map(|rule| rule(value))
}