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