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_c3c3610c70409ee6 {
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>(
41 a: A
42 ) -> Apply!(
43 brand: Self,
44 signature: ('a, A: 'a) -> 'a,
45 );
46}
47
48/// The value wrapped in the context.
49///
50/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
51///
52/// ### Type Signature
53///
54/// `forall a. Pointed f => a -> f a`
55///
56/// ### Type Parameters
57///
58/// * `Brand`: The brand of the context.
59/// * `A`: The type of the value to wrap.
60///
61/// ### Parameters
62///
63/// * `a`: The value to wrap.
64///
65/// ### Returns
66///
67/// A new context containing the value.
68///
69/// ### Examples
70///
71/// ```
72/// use fp_library::classes::pointed::pure;
73/// use fp_library::brands::OptionBrand;
74///
75/// let x = pure::<OptionBrand, _>(5);
76/// assert_eq!(x, Some(5));
77/// ```
78pub fn pure<'a, Brand: Pointed, A: 'a>(
79 a: A
80) -> Apply!(
81 brand: Brand,
82 signature: ('a, A: 'a) -> 'a,
83) {
84 Brand::pure(a)
85}