OptionWitness

Struct OptionWitness 

Source
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

Source§

fn pure<T>(value: T) -> <OptionWitness as HKT>::Type<T>

Lifts a pure value into a Some variant of Option.

§Arguments
  • value: The value to wrap in Some.
§Returns

Some(value).

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,

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: An Option containing the function.
  • f_a: An Option containing the argument.
§Returns

An Option containing the result of the application, or None.

Source§

impl Foldable<OptionWitness> for OptionWitness

Source§

fn fold<A, B, Func>(fa: Option<A>, init: B, f: Func) -> B
where 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: The Option to fold.
  • init: The initial accumulator value.
  • f: The folding function.
§Returns

The accumulated result.

Source§

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,

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: The Option to map over.
  • f: The function to apply to the value inside the Option.
§Returns

A new Option with the function applied to its content, or None.

Source§

impl HKT for OptionWitness

Source§

type Type<T> = Option<T>

Specifies that OptionWitness represents the Option<T> type constructor.

Source§

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>
where Func: FnOnce(A) -> <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 initial Option.
  • f: A function that takes the inner value of m_a and returns a new Option.
§Returns

A new Option representing the chained computation.

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.