Skip to main content

fp_library/classes/
pointed.rs

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