pub trait Functor<F: HKT> {
// Required method
fn fmap<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>
where Func: FnMut(A) -> B;
}Expand description
Re-exports the Functor trait for mapping over type constructors.
The Functor trait abstracts over types that can be mapped over.
It provides the fmap operation, which applies a function to the value
inside a type constructor, preserving the structure of the type.
This trait is generic over F, which is a Higher-Kinded Type (HKT) witness.
§Laws (Informal)
- Identity:
fmap(id, fa) == fa - Composition:
fmap(f.compose(g), fa) == fmap(f, fmap(g, fa))
§Type Parameters
F: A Higher-Kinded Type (HKT) witness that represents the type constructor (e.g.,OptionWitness,ResultWitness<E>,VecWitness).
Required Methods§
Sourcefn fmap<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>where
Func: FnMut(A) -> B,
fn fmap<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>where
Func: FnMut(A) -> B,
Applies a function f to the value inside the container m_a.
This operation transforms the inner value of the type constructor without changing its overall structure.
§Arguments
m_a: The instance of the type constructor (F::Type<A>) to map over.f: The function to apply to the inner value ofm_a.
§Returns
A new instance of F::Type<B> with the function f applied to the inner value.
§Type Parameters
A: The original type of the value inside the container.B: The new type of the value after applying the functionf.Func: The type of the mapping function, which must beFnMut(A) -> B.
§Examples
use deep_causality_haft::{Functor, OptionWitness, HKT};
let opt_a: Option<<OptionWitness as HKT>::Type<i32>> = Some(Some(5));
let f = |x| x * 2;
let opt_b = OptionWitness::fmap(opt_a.unwrap(), f);
assert_eq!(opt_b, Some(10));
let opt_none: Option<<OptionWitness as HKT>::Type<i32>> = Some(None);
let opt_none_mapped = OptionWitness::fmap(opt_none.unwrap(), f);
assert_eq!(opt_none_mapped, None);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.