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