fp_library/classes/
pointed.rs

1use crate::{
2	classes::ClonableFn,
3	hkt::{Apply0L1T, Kind0L1T},
4};
5
6/// A type class for types that can lift values into a context.
7///
8/// `Pointed` provides the ability to lift a value into a context without
9/// adding any additional structure or effects.
10pub trait Pointed: Kind0L1T {
11	/// Lifts a value into the context.
12	///
13	/// # Type Signature
14	///
15	/// `forall a. Pointed f => a -> f a`
16	///
17	/// # Parameters
18	///
19	/// * `a`: A value to be lifted into the context.
20	///
21	/// # Returns
22	///
23	/// The value wrapped in the context.
24	fn pure<ClonableFnBrand: ClonableFn, A: Clone>(a: A) -> Apply0L1T<Self, A>;
25}
26
27/// Lifts a value into the context.
28///
29/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
30///
31/// # Type Signature
32///
33/// `forall a. Pointed f => a -> f a`
34///
35/// # Parameters
36///
37/// * `a`: A value to be lifted into the context.
38///
39/// # Returns
40///
41/// The value wrapped in the context.
42///
43/// # Examples
44///
45/// ```
46/// use fp_library::{brands::{OptionBrand, RcFnBrand}, functions::pure};
47///
48/// assert_eq!(pure::<RcFnBrand, OptionBrand, _>(5), Some(5));
49/// ```
50pub fn pure<ClonableFnBrand: ClonableFn, Brand: Pointed, A: Clone>(a: A) -> Apply0L1T<Brand, A> {
51	Brand::pure::<ClonableFnBrand, _>(a)
52}