fp_library/classes/
applicative.rs

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