Functor

Trait Functor 

Source
pub trait Functor: TypeApp {
    // Required method
    fn fmap<X, Y, G>(fx: Self::Applied<X>, g: G) -> Self::Applied<Y>
       where G: FnMut(X) -> Y;
}
Expand description

A functor for type constructors.

The : TypeApp bound is a kind signature — it says Functor is for type constructors (* -> *), not a behavioral dependency.

This is a functor in the category-theoretic sense: it maps objects (types) and morphisms (functions) while preserving identity and composition.

Laws (not enforced by type system):

  • Identity: fmap id = id
  • Composition: fmap g ∘ fmap f = fmap (g ∘ f)

§Example

use algebra_core::fix::{TypeApp, Functor};

// A simple container
struct Pair<X>(X, X);

struct PairTag;

impl TypeApp for PairTag {
    type Applied<X> = Pair<X>;
}

impl Functor for PairTag {
    fn fmap<X, Y, G>(fx: Pair<X>, mut g: G) -> Pair<Y>
    where
        G: FnMut(X) -> Y,
    {
        Pair(g(fx.0), g(fx.1))
    }
}

Required Methods§

Source

fn fmap<X, Y, G>(fx: Self::Applied<X>, g: G) -> Self::Applied<Y>
where G: FnMut(X) -> Y,

Map a function over the holes (type parameter positions).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§