pub struct OptionWitness;Expand description
Re-exports OptionWitness, the HKT witness for Option<T>.
OptionWitness is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness
for the Option<T> type constructor. It allows Option to be used with generic
functional programming traits like Functor, Applicative, Foldable, and Monad.
By implementing HKT for OptionWitness, we can write generic functions that operate
on any type that has the “shape” of Option, without knowing the inner type T.
Trait Implementations§
Source§impl Applicative<OptionWitness> for OptionWitness
impl Applicative<OptionWitness> for OptionWitness
Source§fn apply<A, B, Func>(
f_ab: <OptionWitness as HKT>::Type<Func>,
f_a: <OptionWitness as HKT>::Type<A>,
) -> <OptionWitness as HKT>::Type<B>where
Func: FnMut(A) -> B,
fn apply<A, B, Func>(
f_ab: <OptionWitness as HKT>::Type<Func>,
f_a: <OptionWitness as HKT>::Type<A>,
) -> <OptionWitness as HKT>::Type<B>where
Func: FnMut(A) -> B,
Applies a function wrapped in an Option (f_ab) to a value wrapped in an Option (f_a).
If both f_ab and f_a are Some, the function is applied to the value.
If either is None, None is returned.
§Arguments
f_ab: AnOptioncontaining the function.f_a: AnOptioncontaining the argument.
§Returns
An Option containing the result of the application, or None.
Source§impl Foldable<OptionWitness> for OptionWitness
impl Foldable<OptionWitness> for OptionWitness
Source§fn fold<A, B, Func>(fa: Option<A>, init: B, f: Func) -> Bwhere
Func: FnMut(B, A) -> B,
fn fold<A, B, Func>(fa: Option<A>, init: B, f: Func) -> Bwhere
Func: FnMut(B, A) -> B,
Folds (reduces) an Option into a single value.
If the Option is Some(value), the function f is applied with the initial
accumulator and the value. If the Option is None, the initial accumulator
is returned.
§Arguments
fa: TheOptionto fold.init: The initial accumulator value.f: The folding function.
§Returns
The accumulated result.
Source§impl Functor<OptionWitness> for OptionWitness
impl Functor<OptionWitness> for OptionWitness
Source§fn fmap<A, B, Func>(
m_a: <OptionWitness as HKT>::Type<A>,
f: Func,
) -> <OptionWitness as HKT>::Type<B>where
Func: FnOnce(A) -> B,
fn fmap<A, B, Func>(
m_a: <OptionWitness as HKT>::Type<A>,
f: Func,
) -> <OptionWitness as HKT>::Type<B>where
Func: FnOnce(A) -> B,
Implements the fmap operation for Option<T>.
If the Option is Some(value), the function f is applied to value,
and the result is wrapped in Some. If the Option is None, None is returned.
§Arguments
m_a: TheOptionto map over.f: The function to apply to the value inside theOption.
§Returns
A new Option with the function applied to its content, or None.
Source§impl HKT for OptionWitness
impl HKT for OptionWitness
Source§impl Monad<OptionWitness> for OptionWitness
impl Monad<OptionWitness> for OptionWitness
Source§fn bind<A, B, Func>(
m_a: <OptionWitness as HKT>::Type<A>,
f: Func,
) -> <OptionWitness as HKT>::Type<B>
fn bind<A, B, Func>( m_a: <OptionWitness as HKT>::Type<A>, f: Func, ) -> <OptionWitness as HKT>::Type<B>
Implements the bind (or and_then) operation for Option<T>.
If the Option is Some(value), the function f is applied to value,
which itself returns an Option. If the Option is None, None is returned.
This effectively chains computations that might fail.
§Arguments
m_a: The initialOption.f: A function that takes the inner value ofm_aand returns a newOption.
§Returns
A new Option representing the chained computation.