VecWitness

Struct VecWitness 

Source
pub struct VecWitness;
Expand description

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

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

Trait Implementations§

Source§

impl Applicative<VecWitness> for VecWitness

Source§

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

Lifts a pure value into a Vec containing only that value.

§Arguments
  • value: The value to wrap in a Vec.
§Returns

A new Vec containing value.

Source§

fn apply<A, B, Func>( f_ab: <VecWitness as HKT>::Type<Func>, f_a: <VecWitness as HKT>::Type<A>, ) -> <VecWitness as HKT>::Type<B>
where Func: FnMut(A) -> B, A: Clone,

Applies a vector of functions (f_ab) to a vector of values (f_a).

Each function in f_ab is applied to each value in f_a, producing a new vector containing all possible combinations of applications.

§Arguments
  • f_ab: A Vec containing functions.
  • f_a: A Vec containing arguments.
§Returns

A new Vec with the results of applying each function to each value.

Source§

impl Foldable<VecWitness> for VecWitness

Source§

fn fold<A, B, Func>(fa: <VecWitness as HKT>::Type<A>, init: B, f: Func) -> B
where <VecWitness as HKT>::Type<A>: IntoIterator<Item = A>, Func: FnMut(B, A) -> B,

Folds (reduces) a Vec into a single value.

Applies the function f cumulatively to the elements of the vector, starting with an initial accumulator value.

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

The accumulated result.

Source§

impl Functor<VecWitness> for VecWitness

Source§

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

Implements the fmap operation for Vec<T>.

Applies the function f to each element in the vector, producing a new vector.

§Arguments
  • m_a: The Vec to map over.
  • f: The function to apply to each element.
§Returns

A new Vec with the function applied to each of its elements.

Source§

impl HKT for VecWitness

Source§

type Type<T> = Vec<T>

Specifies that VecWitness represents the Vec<T> type constructor.

Source§

impl Monad<VecWitness> for VecWitness

Source§

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

Implements the bind (or flat_map) operation for Vec<T>.

Applies the function f to each element in the vector, where f itself returns a new vector. All the resulting vectors are then concatenated into a single Vec. This is useful for chaining computations that produce multiple results.

§Arguments
  • m_a: The initial Vec.
  • f: A function that takes an inner value and returns a new Vec.
§Returns

A new Vec representing the chained and flattened 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.