Skip to main content

BoxWitness

Struct BoxWitness 

Source
pub struct BoxWitness;
Expand description

BoxWitness is a zero-sized type that acts as a Higher-Kinded Type (HKT) witness for the Box<T> type constructor. It allows Box to be used with generic functional programming traits like Functor, Applicative, Foldable, and Monad.

By implementing HKT for BoxWitness, we can write generic functions that operate on any type that has the “shape” of Box, without knowing the inner type T.

§Constraint

BoxWitness uses NoConstraint, meaning it works with any type T.

Trait Implementations§

Source§

impl Applicative<BoxWitness> for BoxWitness

Source§

fn apply<A, B, Func>( f_ab: <BoxWitness as HKT>::Type<Func>, f_a: <BoxWitness as HKT>::Type<A>, ) -> <BoxWitness as HKT>::Type<B>

Applies a function wrapped in a Box (f_ab) to a value wrapped in a Box (f_a).

Source§

impl CoMonad<BoxWitness> for BoxWitness

Source§

fn extract<A>(fa: &<Self as HKT>::Type<A>) -> A

Extracts the value at the current focus of the comonadic context. Read more
Source§

fn extend<A, B, Func>( fa: &<Self as HKT>::Type<A>, f: Func, ) -> <Self as HKT>::Type<B>
where A: Satisfies<NoConstraint> + Clone, B: Satisfies<NoConstraint>, Func: FnMut(&<Self as HKT>::Type<A>) -> B,

Extends the comonadic context by applying a function to its observed state. Read more
Source§

fn duplicate<A>(fa: &F::Type<A>) -> F::Type<F::Type<A>>
where A: Satisfies<F::Constraint> + Clone, F::Type<A>: Satisfies<F::Constraint> + Clone,

Duplicates the comonadic context. Read more
Source§

impl Foldable<BoxWitness> for BoxWitness

Source§

fn fold<A, B, Func>(fa: Box<A>, init: B, f: Func) -> B
where Func: FnMut(B, A) -> B,

Folds (reduces) a Box into a single value.

The function f is applied with the initial accumulator and the value inside the Box.

§Arguments
  • fa: The Box to fold.
  • init: The initial accumulator value.
  • f: The folding function.
§Returns

The accumulated result.

Source§

impl Functor<BoxWitness> for BoxWitness

Source§

fn fmap<A, B, Func>( m_a: <BoxWitness as HKT>::Type<A>, f: Func, ) -> <BoxWitness as HKT>::Type<B>
where A: Satisfies<NoConstraint>, B: Satisfies<NoConstraint>, Func: FnMut(A) -> B,

Implements the fmap operation for Box<T>.

If the Box contains a value, the function f is applied to value, and the result is wrapped in a new Box.

§Arguments
  • m_a: The Box to map over.
  • f: The function to apply to the value inside the Box.
§Returns

A new Box with the function applied to its content.

Source§

impl HKT for BoxWitness

Source§

type Type<T> = Box<T>

Specifies that BoxWitness represents the Box<T> type constructor.

Source§

type Constraint = NoConstraint

The constraint on inner types. Use NoConstraint for fully polymorphic.
Source§

impl Monad<BoxWitness> for BoxWitness

Source§

fn bind<A, B, Func>( m_a: <BoxWitness as HKT>::Type<A>, f: Func, ) -> <BoxWitness as HKT>::Type<B>
where A: Satisfies<NoConstraint>, B: Satisfies<NoConstraint>, Func: FnMut(A) -> <BoxWitness as HKT>::Type<B>,

Implements the bind operation for Box<T>.

Source§

fn join<A>(m_m_a: F::Type<F::Type<A>>) -> F::Type<A>
where A: Satisfies<F::Constraint>, F::Type<A>: Satisfies<F::Constraint>,

Flatten a nested structure into a single layer. Read more
Source§

impl Pure<BoxWitness> for BoxWitness

Source§

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

Lifts a pure value into a Box.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T, F> AliasCoMonad<F> for T
where T: CoMonad<F>, F: HKT,

Source§

fn observe<A>(fa: &F::Type<A>) -> A
where A: Satisfies<F::Constraint> + Clone,

Alias for extract. Gets the value at the current focus.
Source§

fn propagate<A, B, Func>(fa: &F::Type<A>, f: Func) -> F::Type<B>
where A: Satisfies<F::Constraint> + Clone, B: Satisfies<F::Constraint>, Func: FnMut(&F::Type<A>) -> B,

Alias for extend. Propagates a computation across the context.
Source§

impl<T, F> AliasFoldable<F> for T
where T: Foldable<F>, F: HKT,

Source§

fn reduce<A, B, Func>(fa: F::Type<A>, init: B, f: Func) -> B
where A: Satisfies<F::Constraint>, Func: FnMut(B, A) -> B,

Alias for fold. Reduces elements to a single value.
Source§

impl<T, F> AliasFunctor<F> for T
where T: Functor<F>, F: HKT,

Source§

fn transform<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>
where A: Satisfies<F::Constraint>, B: Satisfies<F::Constraint>, Func: FnMut(A) -> B,

Alias for fmap. Transforms the value inside a container.
Source§

impl<T, F> AliasMonad<F> for T
where T: Monad<F>, F: HKT,

Source§

fn chain<A, B, Func>(m_a: F::Type<A>, f: Func) -> F::Type<B>
where A: Satisfies<F::Constraint>, B: Satisfies<F::Constraint>, Func: FnMut(A) -> F::Type<B>,

Alias for bind. Chains a computation, flattening the result.
Source§

fn flatten<A>(m_m_a: F::Type<F::Type<A>>) -> F::Type<A>
where A: Satisfies<F::Constraint>, F::Type<A>: Satisfies<F::Constraint>,

Alias for join. Flattens a nested container.
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.
Source§

impl<T> Satisfies<NoConstraint> for T