Trait Applicative

Source
pub trait Applicative<B>: Functor<B> {
    // Required methods
    fn pure_(value: B) -> Self::M
       where Self: HKT<B, A = B>;
    fn ap<F>(&self, f: <Self as HKT<F>>::M) -> <Self as HKT<B>>::M
       where F: Fn(&<Self as HKT<B>>::A) -> B,
             Self: HKT<F>;
}
Expand description

Applicative type class

Required Methods§

Source

fn pure_(value: B) -> Self::M
where Self: HKT<B, A = B>,

Lift values into the context of the Functor

§Examples
use funlib::Applicative;
let s1 = Option::<i8>::pure_(10);
let s2 = Option::pure_("hi");
let v = Vec::pure_(1);
Source

fn ap<F>(&self, f: <Self as HKT<F>>::M) -> <Self as HKT<B>>::M
where F: Fn(&<Self as HKT<B>>::A) -> B, Self: HKT<F>,

Apply function is almost the same as Functor map. but the function isn’t A => B but A<F => B>

§Examples
use funlib::Applicative;
fn double(i: &i32) -> i32 { i * 2  }
let f: &dyn Fn(&i32) -> i32 = &|x| x * 2;
assert_eq!(Some(4), Some(2).ap(Some(f)));
assert_eq!(Some(4), Some(2).ap(Some(&double)));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<A, B> Applicative<B> for Option<A>

Source§

fn pure_(b: B) -> <Self as HKT<B>>::M

Source§

fn ap<F>(&self, of: <Self as HKT<F>>::M) -> Option<B>
where F: Fn(&A) -> B,

Source§

impl<A, B> Applicative<B> for Box<A>

Source§

fn pure_(b: B) -> <Self as HKT<B>>::M

Source§

fn ap<F>(&self, of: <Self as HKT<F>>::M) -> Box<B>
where F: Fn(&A) -> B,

Source§

impl<A, B> Applicative<B> for Rc<A>

Source§

fn pure_(b: B) -> <Self as HKT<B>>::M

Source§

fn ap<F>(&self, of: <Self as HKT<F>>::M) -> Rc<B>
where F: Fn(&A) -> B,

Source§

impl<A, B> Applicative<B> for Vec<A>

Source§

fn pure_(b: B) -> <Self as HKT<B>>::M

Source§

fn ap<F>(&self, of: <Self as HKT<F>>::M) -> Vec<B>
where F: Fn(&A) -> B,

Implementors§