fp_library/classes/pointed.rs
1//! Pointed type class.
2//!
3//! This module defines the [`Pointed`] trait, which represents a context that can be initialized with a value.
4
5use crate::{Apply, kinds::*};
6
7/// A type class for types that can be constructed from a single value.
8///
9/// `Pointed` represents a context that can be initialized with a value.
10pub trait Pointed: Kind_cdc7cd43dac7585f {
11 /// The value wrapped in the context.
12 ///
13 /// This method wraps a value in a context.
14 ///
15 /// ### Type Signature
16 ///
17 /// `forall a. Pointed f => a -> f a`
18 ///
19 /// ### Type Parameters
20 ///
21 /// * `A`: The type of the value to wrap.
22 ///
23 /// ### Parameters
24 ///
25 /// * `a`: The value to wrap.
26 ///
27 /// ### Returns
28 ///
29 /// A new context containing the value.
30 ///
31 /// ### Examples
32 ///
33 /// ```
34 /// use fp_library::classes::pointed::Pointed;
35 /// use fp_library::brands::OptionBrand;
36 ///
37 /// let x = OptionBrand::pure(5);
38 /// assert_eq!(x, Some(5));
39 /// ```
40 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>);
41}
42
43/// The value wrapped in the context.
44///
45/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
46///
47/// ### Type Signature
48///
49/// `forall a. Pointed f => a -> f a`
50///
51/// ### Type Parameters
52///
53/// * `Brand`: The brand of the context.
54/// * `A`: The type of the value to wrap.
55///
56/// ### Parameters
57///
58/// * `a`: The value to wrap.
59///
60/// ### Returns
61///
62/// A new context containing the value.
63///
64/// ### Examples
65///
66/// ```
67/// use fp_library::classes::pointed::pure;
68/// use fp_library::brands::OptionBrand;
69///
70/// let x = pure::<OptionBrand, _>(5);
71/// assert_eq!(x, Some(5));
72/// ```
73pub fn pure<'a, Brand: Pointed, A: 'a>(
74 a: A
75) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
76 Brand::pure(a)
77}