fp_library/classes/
pointed.rs

1use crate::{Apply, kinds::*};
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: Kind_c3c3610c70409ee6 {
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>(
31		a: A
32	) -> Apply!(
33		brand: Self,
34		signature: ('a, A: 'a) -> 'a,
35	);
36}
37
38/// The value wrapped in the context.
39///
40/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
41///
42/// # Type Signature
43///
44/// `forall a. Pointed f => a -> f a`
45///
46/// # Parameters
47///
48/// * `a`: The value to wrap.
49///
50/// # Returns
51///
52/// A new context containing the value.
53///
54/// # Examples
55///
56/// ```
57/// use fp_library::classes::pointed::pure;
58/// use fp_library::brands::OptionBrand;
59///
60/// let x = pure::<OptionBrand, _>(5);
61/// assert_eq!(x, Some(5));
62/// ```
63pub fn pure<'a, Brand: Pointed, A: 'a>(
64	a: A
65) -> Apply!(
66	brand: Brand,
67	signature: ('a, A: 'a) -> 'a,
68) {
69	Brand::pure(a)
70}