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