fp_library/classes/applicative.rs
1use super::{
2 apply_first::ApplyFirst, apply_second::ApplySecond, pointed::Pointed,
3 semiapplicative::Semiapplicative,
4};
5
6/// A type class for applicative functors.
7///
8/// `Applicative` extends [`Pointed`] and [`Semiapplicative`].
9/// It allows for values to be wrapped in a context and for functions within a context to be applied to values within a context.
10///
11/// # Type Signature
12///
13/// `class (Pointed f, Semiapplicative f) => Applicative f`
14///
15/// # Examples
16///
17/// ```
18/// use fp_library::classes::applicative::Applicative;
19/// use fp_library::classes::pointed::pure;
20/// use fp_library::classes::semiapplicative::apply;
21/// use fp_library::classes::clonable_fn::ClonableFn;
22/// use fp_library::brands::OptionBrand;
23/// use fp_library::brands::RcFnBrand;
24///
25/// // Applicative combines Pointed (pure) and Semiapplicative (apply)
26/// let f = pure::<OptionBrand, _>(<RcFnBrand as ClonableFn>::new(|x: i32| x * 2));
27/// let x = pure::<OptionBrand, _>(5);
28/// let y = apply::<OptionBrand, _, _, RcFnBrand>(f, x);
29/// assert_eq!(y, Some(10));
30/// ```
31pub trait Applicative: Pointed + Semiapplicative + ApplyFirst + ApplySecond {}
32
33impl<Brand> Applicative for Brand where Brand: Pointed + Semiapplicative + ApplyFirst + ApplySecond {}