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