Skip to main content

fp_library/classes/
traversable.rs

1//! Data structures that can be traversed, accumulating results in an applicative context.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{functions::*, brands::*};
7//!
8//! let x = Some(5);
9//! let y = traverse::<OptionBrand, _, _, OptionBrand, _>(|a| Some(a * 2), x);
10//! assert_eq!(y, Some(Some(10)));
11//! ```
12
13use super::{Applicative, Foldable, Functor};
14use crate::{Apply, functions::identity, kinds::*};
15use fp_macros::doc_params;
16use fp_macros::doc_type_params;
17use fp_macros::hm_signature;
18
19/// A type class for traversable functors.
20///
21/// `Traversable` functors can be traversed, which accumulates results and effects in some [`Applicative`] context.
22pub trait Traversable: Functor + Foldable {
23	/// Map each element of the [`Traversable`] structure to a computation, evaluate those computations and combine the results into an [`Applicative`] context.
24	///
25	/// The default implementation is defined in terms of [`sequence`] and [`map`](crate::functions::map).
26	///
27	/// **Note**: This default implementation may be less efficient than a direct implementation because it performs two passes:
28	/// first mapping the function to create an intermediate structure of computations, and then sequencing that structure.
29	/// A direct implementation can often perform the traversal in a single pass without allocating an intermediate container.
30	/// Types should provide their own implementation if possible.
31	///
32	/// ### Type Signature
33	///
34	#[hm_signature(Traversable)]
35	///
36	/// ### Type Parameters
37	///
38	#[doc_type_params(
39		"The lifetime of the elements.",
40		"The type of the elements in the traversable structure.",
41		"The type of the elements in the resulting traversable structure.",
42		"The applicative context.",
43		"The type of the function to apply."
44	)]
45	///
46	/// ### Parameters
47	///
48	#[doc_params(
49		"The function to apply to each element, returning a value in an applicative context.",
50		"The traversable structure."
51	)]
52	///
53	/// ### Returns
54	///
55	/// The traversable structure wrapped in the applicative context.
56	///
57	/// ### Examples
58	///
59	/// ```
60	/// use fp_library::{functions::*, brands::*};
61	///
62	/// let x = Some(5);
63	/// let y = traverse::<OptionBrand, _, _, OptionBrand, _>(|a| Some(a * 2), x);
64	/// assert_eq!(y, Some(Some(10)));
65	/// ```
66	fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
67		func: Func,
68		ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
69	) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
70	where
71		Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
72		Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
73		Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
74	{
75		Self::sequence::<B, F>(Self::map::<
76			A,
77			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
78			Func,
79		>(func, ta))
80	}
81
82	/// Evaluate each computation in a [`Traversable`] structure and accumulate the results into an [`Applicative`] context.
83	///
84	/// The default implementation is defined in terms of [`traverse`] and [`identity`].
85	///
86	/// ### Type Signature
87	///
88	#[hm_signature(Traversable)]
89	///
90	/// ### Type Parameters
91	///
92	#[doc_type_params(
93		"The lifetime of the elements.",
94		"The type of the elements in the traversable structure.",
95		"The applicative context."
96	)]
97	///
98	/// ### Parameters
99	///
100	#[doc_params("The traversable structure containing values in an applicative context.")]
101	///
102	/// ### Returns
103	///
104	/// The traversable structure wrapped in the applicative context.
105	///
106	/// ### Examples
107	///
108	/// ```
109	/// use fp_library::{functions::*, brands::*};
110	///
111	/// let x = Some(Some(5));
112	/// let y = sequence::<OptionBrand, _, OptionBrand>(x);
113	/// assert_eq!(y, Some(Some(5)));
114	/// ```
115	fn sequence<'a, A: 'a + Clone, F: Applicative>(
116		ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
117	) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
118	where
119		Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
120		Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
121	{
122		Self::traverse::<Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>), A, F, _>(
123			identity, ta,
124		)
125	}
126}
127
128/// Map each element of the [`Traversable`] structure to a computation, evaluate those computations and combine the results into an [`Applicative`] context.
129///
130/// Free function version that dispatches to [the type class' associated function][`Traversable::traverse`].
131///
132/// ### Type Signature
133///
134#[hm_signature(Traversable)]
135///
136/// ### Type Parameters
137///
138#[doc_type_params(
139	"The lifetime of the elements.",
140	"The brand of the traversable structure.",
141	"The type of the elements in the traversable structure.",
142	"The type of the elements in the resulting traversable structure.",
143	"The applicative context.",
144	"The type of the function to apply."
145)]
146///
147/// ### Parameters
148///
149#[doc_params(
150	"The function to apply to each element, returning a value in an applicative context.",
151	"The traversable structure."
152)]
153///
154/// ### Returns
155///
156/// The traversable structure wrapped in the applicative context.
157///
158/// ### Examples
159///
160/// ```
161/// use fp_library::{functions::*, brands::*};
162///
163/// let x = Some(5);
164/// let y = traverse::<OptionBrand, _, _, OptionBrand, _>(|a| Some(a * 2), x);
165/// assert_eq!(y, Some(Some(10)));
166/// ```
167pub fn traverse<'a, Brand: Traversable, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
168	func: Func,
169	ta: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
170) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
171where
172	Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
173	Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
174	Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
175{
176	Brand::traverse::<A, B, F, Func>(func, ta)
177}
178
179/// Evaluate each computation in a [`Traversable`] structure and accumulate the results into an [`Applicative`] context.
180///
181/// Free function version that dispatches to [the type class' associated function][`Traversable::sequence`].
182///
183/// ### Type Signature
184///
185#[hm_signature(Traversable)]
186///
187/// ### Type Parameters
188///
189#[doc_type_params(
190	"The lifetime of the elements.",
191	"The brand of the traversable structure.",
192	"The type of the elements in the traversable structure.",
193	"The applicative context."
194)]
195///
196/// ### Parameters
197///
198#[doc_params("The traversable structure containing values in an applicative context.")]
199///
200/// ### Returns
201///
202/// The traversable structure wrapped in the applicative context.
203///
204/// ### Examples
205///
206/// ```
207/// use fp_library::{functions::*, brands::*};
208///
209/// let x = Some(Some(5));
210/// let y = sequence::<OptionBrand, _, OptionBrand>(x);
211/// assert_eq!(y, Some(Some(5)));
212/// ```
213pub fn sequence<'a, Brand: Traversable, A: 'a + Clone, F: Applicative>(
214	ta: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
215) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
216where
217	Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
218	Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
219{
220	Brand::sequence::<A, F>(ta)
221}