fp_library/classes/applicative.rs
1//! Applicative functors, allowing for values and functions to be wrapped and applied within a context.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{
7//! brands::*,
8//! classes::*,
9//! functions::*,
10//! };
11//!
12//! // Applicative combines Pointed (pure) and Semiapplicative (apply)
13//! let f = pure::<OptionBrand, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
14//! let x = pure::<OptionBrand, _>(5);
15//! let y = apply::<RcFnBrand, OptionBrand, _, _>(f, x);
16//! assert_eq!(y, Some(10));
17//! ```
18
19#[fp_macros::document_module]
20mod inner {
21 use {
22 crate::classes::*,
23 fp_macros::*,
24 };
25
26 /// 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.
27 ///
28 /// `class (Pointed f, Semiapplicative f) => Applicative f`
29 #[document_examples]
30 ///
31 /// ```
32 /// use fp_library::{
33 /// brands::*,
34 /// classes::*,
35 /// functions::*,
36 /// };
37 ///
38 /// // Applicative combines Pointed (pure) and Semiapplicative (apply)
39 /// let f = pure::<OptionBrand, _>(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
40 /// let x = pure::<OptionBrand, _>(5);
41 /// let y = apply::<RcFnBrand, OptionBrand, _, _>(f, x);
42 /// assert_eq!(y, Some(10));
43 /// ```
44 pub trait Applicative: Pointed + Semiapplicative + ApplyFirst + ApplySecond {}
45
46 /// Blanket implementation of [`Applicative`].
47 #[document_type_parameters("The brand type.")]
48 impl<Brand> Applicative for Brand where Brand: Pointed + Semiapplicative + ApplyFirst + ApplySecond {}
49}
50
51pub use inner::*;