Trait naan::apply::Apply

source ·
pub trait Apply<F, AB>where
    Self: Functor<F, AB>,
    F: HKT1<T<AB> = Self>,{
    // Required method
    fn apply_with<A, B, Cloner>(self, a: F::T<A>, cloner: Cloner) -> F::T<B>
       where AB: F1<A, Ret = B>,
             Cloner: for<'a> F1<&'a A, Ret = A>;

    // Provided method
    fn apply<A, B>(self, a: F::T<A>) -> F::T<B>
       where Self: Sized,
             AB: F1<A, Ret = B>,
             A: Clone { ... }
}
Expand description

Apply generalizes Functor to any arity.

In other words, you can lift a function

A -> B -> C -> D

to

F<A> -> F<B> -> F<C> -> F<D>.

Example

use naan::prelude::*;

fn maybe_n() -> Option<usize> {
  Some(1)
}

fn maybe_string() -> Option<String> {
  None
}

fn maybe_bytes() -> Option<Vec<u8>> {
  None
}

fn combine_many_things(n: usize, string: String, bytes: Vec<u8>) {
  // ...
}

Some(combine_many_things.curry()).apply(maybe_n())
                                 .apply(maybe_string())
                                 .apply(maybe_bytes());

// with `if let`:
if let Some(n) = maybe_n() {
  if let Some(s) = maybe_string() {
    if let Some(bs) = maybe_bytes() {
      combine_many_things(n, s, bs);
    }
  }
}

// with `let else`:
let Some(n) = maybe_n() else {return};
let Some(s) = maybe_string() else {return};
let Some(bs) = maybe_bytes() else {return};
combine_many_things(n, s, bs);

// with `match`:
match (maybe_n(), maybe_string(), maybe_bytes()) {
  | (Some(n), Some(s), Some(bs)) => combine_many_things(n, s, bs),
  | _ => (),
}

Required Methods§

source

fn apply_with<A, B, Cloner>(self, a: F::T<A>, cloner: Cloner) -> F::T<B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

Apply the function A -> B contained in Self (F<A -> B>) to an instance of F<A> to get F<B>.

Provided Methods§

source

fn apply<A, B>(self, a: F::T<A>) -> F::T<B>where Self: Sized, AB: F1<A, Ret = B>, A: Clone,

See [Apply.apply_with]

Implementations on Foreign Types§

source§

impl<AB, E> Apply<ResultOk<E>, AB> for Result<AB, E>

source§

fn apply_with<A, B, Cloner>( self, a: <ResultOk<E> as HKT1>::T<A>, _: Cloner ) -> <ResultOk<E> as HKT1>::T<B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

source§

impl<const N: usize, AB> Apply<ArrayVec<N>, AB> for ArrayVec<[Option<AB>; N]>

source§

fn apply_with<A, B, Cloner>( self, as_: ArrayVec<[Option<A>; N]>, clone: Cloner ) -> ArrayVec<[Option<B>; N]>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

source§

impl<K, AB> Apply<BTreeMapValues<K>, AB> for BTreeMap<K, AB>where K: Ord,

source§

fn apply_with<A, B, Cloner>( self, as_: BTreeMap<K, A>, cloner: Cloner ) -> BTreeMap<K, B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

source§

impl<AB> Apply<Vec, AB> for Vec<AB>

source§

fn apply_with<A, B, Cloner>( self, a: <Vec as HKT1>::T<A>, cloner: Cloner ) -> <Vec as HKT1>::T<B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

source§

impl<K, AB> Apply<HashMapValues<K>, AB> for HashMap<K, AB>where K: Eq + Hash,

source§

fn apply_with<A, B, Cloner>( self, as_: HashMap<K, A>, cloner: Cloner ) -> HashMap<K, B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

source§

impl<AB> Apply<Option, AB> for Option<AB>

source§

fn apply_with<A, B, Cloner>( self, a: <Option as HKT1>::T<A>, _: Cloner ) -> <Option as HKT1>::T<B>where AB: F1<A, Ret = B>, Cloner: for<'a> F1<&'a A, Ret = A>,

Implementors§

source§

impl<AB> Apply<Id, AB> for Id<AB>