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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! Data structures that can be traversed and filtered simultaneously in an applicative context.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::explicit::*,
//! };
//!
//! let x = Some(5);
//! let y = wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(
//! |a| Some(if a > 2 { Some(a * 2) } else { None }),
//! x,
//! );
//! assert_eq!(y, Some(Some(10)));
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::{
classes::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for data structures that can be traversed and filtered.
///
/// `Witherable` extends [`Filterable`] and [`Traversable`], adding methods for:
/// * `wither`: Effectful `filter_map`.
/// * `wilt`: Effectful `partition_map`.
///
/// ### Laws
///
/// `Witherable` instances must satisfy the following laws:
/// * Identity: `wither(|a| pure(Some(a)), fa) = pure(fa)`.
/// * Multipass (filter): `wither(p, fa) = map(|r| compact(r), traverse(p, fa))`.
/// * Multipass (partition): `wilt(p, fa) = map(|r| separate(r), traverse(p, fa))`.
///
/// Superclass equivalences:
/// * `filter_map(p, fa) = unwrap(wither(|a| Identity(p(a)), fa))`.
/// * `partition_map(p, fa) = unwrap(wilt(|a| Identity(p(a)), fa))`.
/// * `traverse(f, fa) = wither(|a| map(Some, f(a)), fa)`.
#[document_examples]
///
/// Witherable laws for [`Option`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{
/// explicit::{
/// compact,
/// map,
/// separate,
/// traverse,
/// wilt,
/// wither,
/// },
/// *,
/// },
/// };
///
/// // Identity: wither(|a| pure(Some(a)), fa) = pure(fa)
/// assert_eq!(
/// wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(|a| Some(Some(a)), Some(5)),
/// Some(Some(5)),
/// );
/// assert_eq!(
/// wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(|a| Some(Some(a)), None::<i32>),
/// Some(None),
/// );
///
/// // Multipass (filter): wither(p, fa) = map(|r| compact(r), traverse(p, fa))
/// let p = |a: i32| Some(if a > 2 { Some(a * 2) } else { None });
/// assert_eq!(
/// wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(p, Some(5)),
/// map::<OptionBrand, _, _, _, _>(
/// |r| compact::<OptionBrand, _, _, _>(r),
/// traverse::<RcFnBrand, OptionBrand, _, _, OptionBrand, _, _>(p, Some(5)),
/// ),
/// );
///
/// // Multipass (partition): wilt(p, fa) = map(|r| separate(r), traverse(p, fa))
/// let p = |a: i32| Some(if a > 2 { Ok(a) } else { Err(a) });
/// assert_eq!(
/// wilt::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _, _>(p, Some(5)),
/// map::<OptionBrand, _, _, _, _>(
/// |r| separate::<OptionBrand, _, _, _, _>(r),
/// traverse::<RcFnBrand, OptionBrand, _, _, OptionBrand, _, _>(p, Some(5)),
/// ),
/// );
/// ```
///
/// Witherable laws for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{
/// explicit::{
/// compact,
/// map,
/// traverse,
/// wither,
/// },
/// *,
/// },
/// };
///
/// // Identity: wither(|a| pure(Some(a)), fa) = pure(fa)
/// assert_eq!(
/// wither::<RcFnBrand, VecBrand, OptionBrand, _, _, _, _>(|a| Some(Some(a)), vec![1, 2, 3]),
/// Some(vec![1, 2, 3]),
/// );
///
/// // Multipass (filter): wither(p, fa) = map(|r| compact(r), traverse(p, fa))
/// let p = |a: i32| Some(if a > 2 { Some(a * 2) } else { None });
/// assert_eq!(
/// wither::<RcFnBrand, VecBrand, OptionBrand, _, _, _, _>(p, vec![1, 2, 3, 4, 5]),
/// map::<OptionBrand, _, _, _, _>(
/// |r| compact::<VecBrand, _, _, _>(r),
/// traverse::<RcFnBrand, VecBrand, _, _, OptionBrand, _, _>(p, vec![1, 2, 3, 4, 5]),
/// ),
/// );
/// ```
///
/// ### Minimal Implementation
///
/// A minimal implementation of `Witherable` requires no specific method implementations, as all methods have default implementations based on [`Traversable`] and [`Compactable`](crate::classes::compactable::Compactable).
///
/// However, it is recommended to implement [`Witherable::wilt`] and [`Witherable::wither`] to avoid the intermediate structure created by the default implementations (which use [`traverse`](crate::functions::traverse) followed by [`separate`](crate::functions::separate) or [`compact`](crate::functions::compact)).
pub trait Witherable: Filterable + Traversable {
/// Partitions a data structure based on a function that returns a [`Result`] in an applicative context.
///
/// The default implementation uses [`traverse`](crate::functions::traverse) and [`separate`](crate::functions::separate).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters(
"The function to apply to each element, returning a [`Result`] in an applicative context.",
"The data structure to partition."
)]
///
#[document_returns("The partitioned data structure wrapped in the applicative context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// types::*,
/// };
///
/// let x = Some(5);
/// let y = wilt::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _, _>(
/// |a| Some(if a > 2 { Ok(a) } else { Err(a) }),
/// x,
/// );
/// assert_eq!(y, Some((None, Some(5))));
/// ```
fn wilt<'a, M: Applicative, A: 'a + Clone, E: 'a + Clone, O: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
+ 'a,
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
(
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
),
>)
where
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone, {
M::map(
|res| Self::separate::<E, O>(res),
Self::traverse::<A, Result<O, E>, M>(func, ta),
)
}
/// Maps a function over a data structure and filters out [`None`] results in an applicative context.
///
/// The default implementation uses [`traverse`](crate::functions::traverse) and [`compact`](crate::functions::compact).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the elements in the output structure."
)]
///
#[document_parameters(
"The function to apply to each element, returning an [`Option`] in an applicative context.",
"The data structure to filter and map."
)]
///
#[document_returns(
"The filtered and mapped data structure wrapped in the applicative context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(
/// |a| Some(if a > 2 { Some(a * 2) } else { None }),
/// x,
/// );
/// assert_eq!(y, Some(Some(10)));
/// ```
fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M 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, Option<B>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone, {
M::map(|opt| Self::compact(opt), Self::traverse::<A, Option<B>, M>(func, ta))
}
}
/// Partitions a data structure based on a function that returns a [`Result`] in an applicative context.
///
/// Free function version that dispatches to [the type class' associated function][`Witherable::wilt`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the witherable structure.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters(
"The function to apply to each element, returning a [`Result`] in an applicative context.",
"The data structure to partition."
)]
///
#[document_returns("The partitioned data structure wrapped in the applicative context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// types::*,
/// };
///
/// let x = Some(5);
/// let y = wilt::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _, _>(
/// |a| Some(if a > 2 { Ok(a) } else { Err(a) }),
/// x,
/// );
/// assert_eq!(y, Some((None, Some(5))));
/// ```
pub fn wilt<'a, F: Witherable, M: Applicative, A: 'a + Clone, E: 'a + Clone, O: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>) + 'a,
ta: Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
(
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
),
>)
where
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone, {
F::wilt::<M, A, E, O>(func, ta)
}
/// Maps a function over a data structure and filters out [`None`] results in an applicative context.
///
/// Free function version that dispatches to [the type class' associated function][`Witherable::wither`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the witherable structure.",
"The applicative context.",
"The type of the elements in the input structure.",
"The type of the elements in the output structure."
)]
///
#[document_parameters(
"The function to apply to each element, returning an [`Option`] in an applicative context.",
"The data structure to filter and map."
)]
///
#[document_returns(
"The filtered and mapped data structure wrapped in the applicative context."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(
/// |a| Some(if a > 2 { Some(a * 2) } else { None }),
/// x,
/// );
/// assert_eq!(y, Some(Some(10)));
/// ```
pub fn wither<'a, F: Witherable, M: Applicative, A: 'a + Clone, B: 'a + Clone>(
func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
ta: Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
>)
where
Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone, {
F::wither::<M, A, B>(func, ta)
}
}
pub use inner::*;