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§
Sourcefn pure_(value: B) -> Self::Mwhere
Self: HKT<B, A = B>,
fn pure_(value: B) -> Self::Mwhere
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);
Sourcefn ap<F>(&self, f: <Self as HKT<F>>::M) -> <Self as HKT<B>>::M
fn ap<F>(&self, f: <Self as HKT<F>>::M) -> <Self as HKT<B>>::M
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.