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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Data structures that can be filtered and partitioned based on predicates or mapping functions.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::explicit::*,
//! };
//!
//! let x = Some(5);
//! let y = filter::<OptionBrand, _, _, _>(|a| a > 2, x);
//! assert_eq!(y, Some(5));
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::{
classes::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for data structures that can be filtered and partitioned.
///
/// `Filterable` extends [`Compactable`] and [`Functor`], adding methods for:
/// * `filter`: Keeping elements that satisfy a predicate.
/// * `filter_map`: Mapping and filtering in one step.
/// * `partition`: Splitting elements based on a predicate.
/// * `partition_map`: Mapping and partitioning in one step.
///
/// ### Laws
///
/// `Filterable` instances must satisfy the following laws:
/// * Distributivity: `filter_map(identity, fa) = compact(fa)`.
/// * Distributivity: `partition_map(identity, fa) = separate(fa)`.
/// * Identity: `filter_map(Some, fa) = fa`.
/// * Composition: `filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))`.
/// * Consistency (`filter`/`filter_map`): `filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)`.
/// * Consistency (`partition`/`partition_map`): `partition(p, fa) = partition_map(|a| if p(a) { Ok(a) } else { Err(a) }, fa)`.
#[document_examples]
///
/// Filterable laws for [`Option`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{*, explicit::{compact, filter, filter_map, partition_map, separate}},
/// };
///
/// // Distributivity: filter_map(identity, fa) = compact(fa)
/// let fa: Option<Option<i32>> = Some(Some(5));
/// assert_eq!(
/// filter_map::<OptionBrand, _, _, _, _>(identity, fa),
/// compact::<OptionBrand, _, _, _>(fa),
/// );
///
/// // Distributivity: partition_map(identity, fa) = separate(fa)
/// let fa: Option<Result<i32, &str>> = Some(Ok(5));
/// assert_eq!(
/// partition_map::<OptionBrand, _, _, _, _, _>(identity, fa),
/// separate::<OptionBrand, _, _, _, _>(fa),
/// );
///
/// // Identity: filter_map(Some, fa) = fa
/// assert_eq!(filter_map::<OptionBrand, _, _, _, _>(Some, Some(5)), Some(5));
/// assert_eq!(filter_map::<OptionBrand, _, _, _, _>(Some, None::<i32>), None);
///
/// // Composition: filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))
/// let l = |x: i32| if x > 3 { Some(x * 10) } else { None };
/// let r = |x: i32| if x > 1 { Some(x + 1) } else { None };
/// assert_eq!(
/// filter_map::<OptionBrand, _, _, _, _>(|a| r(a).and_then(l), Some(5)),
/// filter_map::<OptionBrand, _, _, _, _>(l, filter_map::<OptionBrand, _, _, _, _>(r, Some(5))),
/// );
///
/// // Consistency (filter/filter_map): filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)
/// let p = |x: i32| x > 3;
/// assert_eq!(
/// filter::<OptionBrand, _, _, _>(p, Some(5)),
/// filter_map::<OptionBrand, _, _, _, _>(|a: i32| if p(a) { Some(a) } else { None }, Some(5)),
/// );
/// ```
///
/// Filterable laws for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{*, explicit::{compact, filter, filter_map}},
/// };
///
/// // Distributivity: filter_map(identity, fa) = compact(fa)
/// let fa: Vec<Option<i32>> = vec![Some(1), None, Some(3)];
/// assert_eq!(
/// filter_map::<VecBrand, _, _, _, _>(identity, fa.clone()),
/// compact::<VecBrand, _, _, _>(fa),
/// );
///
/// // Identity: filter_map(Some, fa) = fa
/// assert_eq!(
/// filter_map::<VecBrand, _, _, _, _>(Some, vec![1, 2, 3]),
/// vec![1, 2, 3],
/// );
///
/// // Composition: filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))
/// let l = |x: i32| if x > 3 { Some(x * 10) } else { None };
/// let r = |x: i32| if x > 1 { Some(x + 1) } else { None };
/// assert_eq!(
/// filter_map::<VecBrand, _, _, _, _>(|a| r(a).and_then(l), vec![1, 2, 3, 4, 5]),
/// filter_map::<VecBrand, _, _, _, _>(l, filter_map::<VecBrand, _, _, _, _>(r, vec![1, 2, 3, 4, 5])),
/// );
///
/// // Consistency (filter/filter_map): filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)
/// let p = |x: i32| x > 3;
/// assert_eq!(
/// filter::<VecBrand, _, _, _>(p, vec![1, 2, 3, 4, 5]),
/// filter_map::<VecBrand, _, _, _, _>(|a: i32| if p(a) { Some(a) } else { None }, vec![1, 2, 3, 4, 5]),
/// );
/// ```
///
/// ### Minimal Implementation
///
/// A minimal implementation of `Filterable` requires no specific method implementations, as all methods have default implementations based on [`Compactable`] and [`Functor`].
///
/// However, it is recommended to implement [`Filterable::partition_map`] and [`Filterable::filter_map`] to avoid the intermediate structure created by the default implementations (which use [`map`](crate::functions::map) followed by [`separate`](crate::functions::separate) or [`compact`](crate::functions::compact)).
///
/// * If [`Filterable::partition_map`] is implemented, [`Filterable::partition`] is derived from it.
/// * If [`Filterable::filter_map`] is implemented, [`Filterable::filter`] is derived from it.
pub trait Filterable: Compactable + Functor {
/// Partitions a data structure based on a function that returns a [`Result`].
///
/// The default implementation uses [`map`](crate::functions::map) and [`separate`](crate::functions::separate).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"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`].",
"The data structure to partition."
)]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let (errs, oks) =
/// partition_map::<OptionBrand, _, _, _, _, _>(|a| if a > 2 { Ok(a) } else { Err(a) }, x);
/// assert_eq!(oks, Some(5));
/// assert_eq!(errs, None);
/// ```
fn partition_map<'a, A: 'a, E: 'a, O: 'a>(
func: impl Fn(A) -> Result<O, E> + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, 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>),
) {
Self::separate::<E, O>(Self::map::<A, Result<O, E>>(func, fa))
}
/// Partitions a data structure based on a predicate.
///
/// The default implementation uses [`partition_map`].
///
/// **Note**: The return order is `(satisfied, not_satisfied)`, matching Rust's [`Iterator::partition`].
/// This is achieved by mapping satisfied elements to [`Ok`] and unsatisfied elements to [`Err`] internally,
/// as `separate` returns `(Oks, Errs)`.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the elements in the structure."
)]
///
#[document_parameters("The predicate function.", "The data structure to partition.")]
///
#[document_returns(
"A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let (not_satisfied, satisfied) = partition::<OptionBrand, _, _, _>(|a| a > 2, x);
/// assert_eq!(satisfied, Some(5));
/// assert_eq!(not_satisfied, None);
/// ```
fn partition<'a, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) {
Self::partition_map(move |a| if func(a.clone()) { Ok(a) } else { Err(a) }, fa)
}
/// Maps a function over a data structure and filters out [`None`] results.
///
/// The default implementation uses [`map`](crate::functions::map) and [`compact`](crate::functions::compact).
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"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`].",
"The data structure to filter and map."
)]
#[document_returns(
"A new data structure containing only the values where the function returned [`Some`]."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = filter_map::<OptionBrand, _, _, _, _>(|a| if a > 2 { Some(a * 2) } else { None }, x);
/// assert_eq!(y, Some(10));
/// ```
fn filter_map<'a, A: 'a, B: 'a>(
func: impl Fn(A) -> Option<B> + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
Self::compact::<B>(Self::map::<A, Option<B>>(func, fa))
}
/// Filters a data structure based on a predicate.
///
/// The default implementation uses [`filter_map`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the elements in the structure."
)]
///
#[document_parameters("The predicate function.", "The data structure to filter.")]
///
#[document_returns(
"A new data structure containing only the elements that satisfy the predicate."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = filter::<OptionBrand, _, _, _>(|a| a > 2, x);
/// assert_eq!(y, Some(5));
/// ```
fn filter<'a, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Self::filter_map(move |a| if func(a.clone()) { Some(a) } else { None }, fa)
}
}
/// Partitions a data structure based on a function that returns a [`Result`].
///
/// Free function version that dispatches to [the type class' associated function][`Filterable::partition_map`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the filterable structure.",
"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`].",
"The data structure to partition."
)]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let (errs, oks) =
/// partition_map::<OptionBrand, _, _, _, _, _>(|a| if a > 2 { Ok(a) } else { Err(a) }, x);
/// assert_eq!(oks, Some(5));
/// assert_eq!(errs, None);
/// ```
pub fn partition_map<'a, Brand: Filterable, A: 'a, E: 'a, O: 'a>(
func: impl Fn(A) -> Result<O, E> + 'a,
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> (
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
) {
Brand::partition_map(func, fa)
}
/// Partitions a data structure based on a predicate.
///
/// Free function version that dispatches to [the type class' associated function][`Filterable::partition`].
///
/// **Note**: The return order is `(satisfied, not_satisfied)`, matching Rust's [`Iterator::partition`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the filterable structure.",
"The type of the elements in the structure."
)]
///
#[document_parameters("The predicate function.", "The data structure to partition.")]
///
#[document_returns(
"A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let (not_satisfied, satisfied) = partition::<OptionBrand, _, _, _>(|a| a > 2, x);
/// assert_eq!(satisfied, Some(5));
/// assert_eq!(not_satisfied, None);
/// ```
pub fn partition<'a, Brand: Filterable, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> (
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) {
Brand::partition(func, fa)
}
/// Maps a function over a data structure and filters out [`None`] results.
///
/// Free function version that dispatches to [the type class' associated function][`Filterable::filter_map`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the filterable structure.",
"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`].",
"The data structure to filter and map."
)]
///
#[document_returns(
"A new data structure containing only the values where the function returned [`Some`]."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = filter_map::<OptionBrand, _, _, _, _>(|a| if a > 2 { Some(a * 2) } else { None }, x);
/// assert_eq!(y, Some(10));
/// ```
pub fn filter_map<'a, Brand: Filterable, A: 'a, B: 'a>(
func: impl Fn(A) -> Option<B> + 'a,
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
Brand::filter_map(func, fa)
}
/// Filters a data structure based on a predicate.
///
/// Free function version that dispatches to [the type class' associated function][`Filterable::filter`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the filterable structure.",
"The type of the elements in the structure."
)]
///
#[document_parameters("The predicate function.", "The data structure to filter.")]
///
#[document_returns(
"A new data structure containing only the elements that satisfy the predicate."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = filter::<OptionBrand, _, _, _>(|a| a > 2, x);
/// assert_eq!(y, Some(5));
/// ```
pub fn filter<'a, Brand: Filterable, A: 'a + Clone>(
func: impl Fn(A) -> bool + 'a,
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Brand::filter(func, fa)
}
}
pub use inner::*;