fp_library/classes/
pointed.rs

1use crate::hkt::{Apply1L1T, Kind1L1T};
2
3/// A type class for types that can be constructed from a single value.
4///
5/// `Pointed` represents a context that can be initialized with a value.
6pub trait Pointed: Kind1L1T {
7	/// The value wrapped in the context.
8	///
9	/// # Type Signature
10	///
11	/// `forall a. Pointed f => a -> f a`
12	///
13	/// # Parameters
14	///
15	/// * `a`: The value to wrap.
16	///
17	/// # Returns
18	///
19	/// A new context containing the value.
20	///
21	/// # Examples
22	///
23	/// ```
24	/// use fp_library::classes::pointed::Pointed;
25	/// use fp_library::brands::OptionBrand;
26	///
27	/// let x = OptionBrand::pure(5);
28	/// assert_eq!(x, Some(5));
29	/// ```
30	fn pure<'a, A: 'a>(a: A) -> Apply1L1T<'a, Self, A>;
31}
32
33/// The value wrapped in the context.
34///
35/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
36///
37/// # Type Signature
38///
39/// `forall a. Pointed f => a -> f a`
40///
41/// # Parameters
42///
43/// * `a`: The value to wrap.
44///
45/// # Returns
46///
47/// A new context containing the value.
48///
49/// # Examples
50///
51/// ```
52/// use fp_library::classes::pointed::pure;
53/// use fp_library::brands::OptionBrand;
54///
55/// let x = pure::<OptionBrand, _>(5);
56/// assert_eq!(x, Some(5));
57/// ```
58pub fn pure<'a, Brand: Pointed, A: 'a>(a: A) -> Apply1L1T<'a, Brand, A> {
59	Brand::pure(a)
60}