Skip to main content

fp_library/classes/
filterable.rs

1//! Data structures that can be filtered and partitioned based on predicates or mapping functions.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{
7//! 	brands::*,
8//! 	functions::*,
9//! };
10//!
11//! let x = Some(5);
12//! let y = filter::<OptionBrand, _>(|a| a > 2, x);
13//! assert_eq!(y, Some(5));
14//! ```
15
16#[fp_macros::document_module]
17mod inner {
18	use {
19		crate::{
20			classes::*,
21			kinds::*,
22		},
23		fp_macros::*,
24	};
25
26	/// A type class for data structures that can be filtered and partitioned.
27	///
28	/// `Filterable` extends [`Compactable`] and [`Functor`], adding methods for:
29	/// *   `filter`: Keeping elements that satisfy a predicate.
30	/// *   `filter_map`: Mapping and filtering in one step.
31	/// *   `partition`: Splitting elements based on a predicate.
32	/// *   `partition_map`: Mapping and partitioning in one step.
33	///
34	/// ### Laws
35	///
36	/// `Filterable` instances must satisfy the following laws:
37	/// * Distributivity: `filter_map(identity, fa) = compact(fa)`.
38	/// * Distributivity: `partition_map(identity, fa) = separate(fa)`.
39	/// * Identity: `filter_map(Some, fa) = fa`.
40	/// * Composition: `filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))`.
41	/// * Consistency (`filter`/`filter_map`): `filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)`.
42	/// * Consistency (`partition`/`partition_map`): `partition(p, fa) = partition_map(|a| if p(a) { Ok(a) } else { Err(a) }, fa)`.
43	#[document_examples]
44	///
45	/// Filterable laws for [`Option`]:
46	///
47	/// ```
48	/// use fp_library::{
49	/// 	brands::*,
50	/// 	functions::*,
51	/// };
52	///
53	/// // Distributivity: filter_map(identity, fa) = compact(fa)
54	/// let fa: Option<Option<i32>> = Some(Some(5));
55	/// assert_eq!(
56	/// 	filter_map::<OptionBrand, _, _>(identity, fa),
57	/// 	compact::<OptionBrand, _>(fa),
58	/// );
59	///
60	/// // Distributivity: partition_map(identity, fa) = separate(fa)
61	/// let fa: Option<Result<i32, &str>> = Some(Ok(5));
62	/// assert_eq!(
63	/// 	partition_map::<OptionBrand, _, _, _>(identity, fa),
64	/// 	separate::<OptionBrand, _, _>(fa),
65	/// );
66	///
67	/// // Identity: filter_map(Some, fa) = fa
68	/// assert_eq!(filter_map::<OptionBrand, _, _>(Some, Some(5)), Some(5));
69	/// assert_eq!(filter_map::<OptionBrand, _, _>(Some, None::<i32>), None);
70	///
71	/// // Composition: filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))
72	/// let l = |x: i32| if x > 3 { Some(x * 10) } else { None };
73	/// let r = |x: i32| if x > 1 { Some(x + 1) } else { None };
74	/// assert_eq!(
75	/// 	filter_map::<OptionBrand, _, _>(|a| r(a).and_then(l), Some(5)),
76	/// 	filter_map::<OptionBrand, _, _>(l, filter_map::<OptionBrand, _, _>(r, Some(5))),
77	/// );
78	///
79	/// // Consistency (filter/filter_map): filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)
80	/// let p = |x: i32| x > 3;
81	/// assert_eq!(
82	/// 	filter::<OptionBrand, _>(p, Some(5)),
83	/// 	filter_map::<OptionBrand, _, _>(|a: i32| if p(a) { Some(a) } else { None }, Some(5)),
84	/// );
85	/// ```
86	///
87	/// Filterable laws for [`Vec`]:
88	///
89	/// ```
90	/// use fp_library::{
91	/// 	brands::*,
92	/// 	functions::*,
93	/// };
94	///
95	/// // Distributivity: filter_map(identity, fa) = compact(fa)
96	/// let fa: Vec<Option<i32>> = vec![Some(1), None, Some(3)];
97	/// assert_eq!(
98	/// 	filter_map::<VecBrand, _, _>(identity, fa.clone()),
99	/// 	compact::<VecBrand, _>(fa),
100	/// );
101	///
102	/// // Identity: filter_map(Some, fa) = fa
103	/// assert_eq!(
104	/// 	filter_map::<VecBrand, _, _>(Some, vec![1, 2, 3]),
105	/// 	vec![1, 2, 3],
106	/// );
107	///
108	/// // Composition: filter_map(|a| r(a).and_then(l), fa) = filter_map(l, filter_map(r, fa))
109	/// let l = |x: i32| if x > 3 { Some(x * 10) } else { None };
110	/// let r = |x: i32| if x > 1 { Some(x + 1) } else { None };
111	/// assert_eq!(
112	/// 	filter_map::<VecBrand, _, _>(|a| r(a).and_then(l), vec![1, 2, 3, 4, 5]),
113	/// 	filter_map::<VecBrand, _, _>(l, filter_map::<VecBrand, _, _>(r, vec![1, 2, 3, 4, 5])),
114	/// );
115	///
116	/// // Consistency (filter/filter_map): filter(p, fa) = filter_map(|a| if p(a) { Some(a) } else { None }, fa)
117	/// let p = |x: i32| x > 3;
118	/// assert_eq!(
119	/// 	filter::<VecBrand, _>(p, vec![1, 2, 3, 4, 5]),
120	/// 	filter_map::<VecBrand, _, _>(|a: i32| if p(a) { Some(a) } else { None }, vec![1, 2, 3, 4, 5]),
121	/// );
122	/// ```
123	///
124	/// ### Minimal Implementation
125	///
126	/// A minimal implementation of `Filterable` requires no specific method implementations, as all methods have default implementations based on [`Compactable`] and [`Functor`].
127	///
128	/// 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)).
129	///
130	/// *   If [`Filterable::partition_map`] is implemented, [`Filterable::partition`] is derived from it.
131	/// *   If [`Filterable::filter_map`] is implemented, [`Filterable::filter`] is derived from it.
132	pub trait Filterable: Compactable + Functor {
133		/// Partitions a data structure based on a function that returns a [`Result`].
134		///
135		/// The default implementation uses [`map`](crate::functions::map) and [`separate`](crate::functions::separate).
136		#[document_signature]
137		///
138		#[document_type_parameters(
139			"The lifetime of the elements.",
140			"The type of the elements in the input structure.",
141			"The type of the error values.",
142			"The type of the success values."
143		)]
144		///
145		#[document_parameters(
146			"The function to apply to each element, returning a [`Result`].",
147			"The data structure to partition."
148		)]
149		///
150		#[document_returns(
151			"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
152		)]
153		#[document_examples]
154		///
155		/// ```
156		/// use fp_library::{
157		/// 	brands::*,
158		/// 	functions::*,
159		/// };
160		///
161		/// let x = Some(5);
162		/// let (errs, oks) =
163		/// 	partition_map::<OptionBrand, _, _, _>(|a| if a > 2 { Ok(a) } else { Err(a) }, x);
164		/// assert_eq!(oks, Some(5));
165		/// assert_eq!(errs, None);
166		/// ```
167		fn partition_map<'a, A: 'a, E: 'a, O: 'a>(
168			func: impl Fn(A) -> Result<O, E> + 'a,
169			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
170		) -> (
171			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
172			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
173		) {
174			Self::separate::<E, O>(Self::map::<A, Result<O, E>>(func, fa))
175		}
176
177		/// Partitions a data structure based on a predicate.
178		///
179		/// The default implementation uses [`partition_map`].
180		///
181		/// **Note**: The return order is `(satisfied, not_satisfied)`, matching Rust's [`Iterator::partition`].
182		/// This is achieved by mapping satisfied elements to [`Ok`] and unsatisfied elements to [`Err`] internally,
183		/// as `separate` returns `(Oks, Errs)`.
184		#[document_signature]
185		///
186		#[document_type_parameters(
187			"The lifetime of the elements.",
188			"The type of the elements in the structure."
189		)]
190		///
191		#[document_parameters("The predicate function.", "The data structure to partition.")]
192		///
193		#[document_returns(
194			"A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
195		)]
196		#[document_examples]
197		///
198		/// ```
199		/// use fp_library::{
200		/// 	brands::*,
201		/// 	functions::*,
202		/// };
203		///
204		/// let x = Some(5);
205		/// let (not_satisfied, satisfied) = partition::<OptionBrand, _>(|a| a > 2, x);
206		/// assert_eq!(satisfied, Some(5));
207		/// assert_eq!(not_satisfied, None);
208		/// ```
209		fn partition<'a, A: 'a + Clone>(
210			func: impl Fn(A) -> bool + 'a,
211			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
212		) -> (
213			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
214			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
215		) {
216			Self::partition_map(move |a| if func(a.clone()) { Ok(a) } else { Err(a) }, fa)
217		}
218
219		/// Maps a function over a data structure and filters out [`None`] results.
220		///
221		/// The default implementation uses [`map`](crate::functions::map) and [`compact`](crate::functions::compact).
222		#[document_signature]
223		///
224		#[document_type_parameters(
225			"The lifetime of the elements.",
226			"The type of the elements in the input structure.",
227			"The type of the elements in the output structure."
228		)]
229		#[document_parameters(
230			"The function to apply to each element, returning an [`Option`].",
231			"The data structure to filter and map."
232		)]
233		#[document_returns(
234			"A new data structure containing only the values where the function returned [`Some`]."
235		)]
236		#[document_examples]
237		///
238		/// ```
239		/// use fp_library::{
240		/// 	brands::*,
241		/// 	functions::*,
242		/// };
243		///
244		/// let x = Some(5);
245		/// let y = filter_map::<OptionBrand, _, _>(|a| if a > 2 { Some(a * 2) } else { None }, x);
246		/// assert_eq!(y, Some(10));
247		/// ```
248		fn filter_map<'a, A: 'a, B: 'a>(
249			func: impl Fn(A) -> Option<B> + 'a,
250			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
251		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
252			Self::compact::<B>(Self::map::<A, Option<B>>(func, fa))
253		}
254
255		/// Filters a data structure based on a predicate.
256		///
257		/// The default implementation uses [`filter_map`].
258		#[document_signature]
259		///
260		#[document_type_parameters(
261			"The lifetime of the elements.",
262			"The type of the elements in the structure."
263		)]
264		///
265		#[document_parameters("The predicate function.", "The data structure to filter.")]
266		///
267		#[document_returns(
268			"A new data structure containing only the elements that satisfy the predicate."
269		)]
270		#[document_examples]
271		///
272		/// ```
273		/// use fp_library::{
274		/// 	brands::*,
275		/// 	functions::*,
276		/// };
277		///
278		/// let x = Some(5);
279		/// let y = filter::<OptionBrand, _>(|a| a > 2, x);
280		/// assert_eq!(y, Some(5));
281		/// ```
282		fn filter<'a, A: 'a + Clone>(
283			func: impl Fn(A) -> bool + 'a,
284			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
285		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
286			Self::filter_map(move |a| if func(a.clone()) { Some(a) } else { None }, fa)
287		}
288	}
289
290	/// Partitions a data structure based on a function that returns a [`Result`].
291	///
292	/// Free function version that dispatches to [the type class' associated function][`Filterable::partition_map`].
293	#[document_signature]
294	///
295	#[document_type_parameters(
296		"The lifetime of the elements.",
297		"The brand of the filterable structure.",
298		"The type of the elements in the input structure.",
299		"The type of the error values.",
300		"The type of the success values."
301	)]
302	///
303	#[document_parameters(
304		"The function to apply to each element, returning a [`Result`].",
305		"The data structure to partition."
306	)]
307	///
308	#[document_returns(
309		"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
310	)]
311	#[document_examples]
312	///
313	/// ```
314	/// use fp_library::{
315	/// 	brands::*,
316	/// 	functions::*,
317	/// };
318	///
319	/// let x = Some(5);
320	/// let (errs, oks) =
321	/// 	partition_map::<OptionBrand, _, _, _>(|a| if a > 2 { Ok(a) } else { Err(a) }, x);
322	/// assert_eq!(oks, Some(5));
323	/// assert_eq!(errs, None);
324	/// ```
325	pub fn partition_map<'a, Brand: Filterable, A: 'a, E: 'a, O: 'a>(
326		func: impl Fn(A) -> Result<O, E> + 'a,
327		fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
328	) -> (
329		Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
330		Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
331	) {
332		Brand::partition_map(func, fa)
333	}
334
335	/// Partitions a data structure based on a predicate.
336	///
337	/// Free function version that dispatches to [the type class' associated function][`Filterable::partition`].
338	///
339	/// **Note**: The return order is `(satisfied, not_satisfied)`, matching Rust's [`Iterator::partition`].
340	#[document_signature]
341	///
342	#[document_type_parameters(
343		"The lifetime of the elements.",
344		"The brand of the filterable structure.",
345		"The type of the elements in the structure."
346	)]
347	///
348	#[document_parameters("The predicate function.", "The data structure to partition.")]
349	///
350	#[document_returns(
351		"A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
352	)]
353	#[document_examples]
354	///
355	/// ```
356	/// use fp_library::{
357	/// 	brands::*,
358	/// 	functions::*,
359	/// };
360	///
361	/// let x = Some(5);
362	/// let (not_satisfied, satisfied) = partition::<OptionBrand, _>(|a| a > 2, x);
363	/// assert_eq!(satisfied, Some(5));
364	/// assert_eq!(not_satisfied, None);
365	/// ```
366	pub fn partition<'a, Brand: Filterable, A: 'a + Clone>(
367		func: impl Fn(A) -> bool + 'a,
368		fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
369	) -> (
370		Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
371		Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
372	) {
373		Brand::partition(func, fa)
374	}
375
376	/// Maps a function over a data structure and filters out [`None`] results.
377	///
378	/// Free function version that dispatches to [the type class' associated function][`Filterable::filter_map`].
379	#[document_signature]
380	///
381	#[document_type_parameters(
382		"The lifetime of the elements.",
383		"The brand of the filterable structure.",
384		"The type of the elements in the input structure.",
385		"The type of the elements in the output structure."
386	)]
387	///
388	#[document_parameters(
389		"The function to apply to each element, returning an [`Option`].",
390		"The data structure to filter and map."
391	)]
392	///
393	#[document_returns(
394		"A new data structure containing only the values where the function returned [`Some`]."
395	)]
396	#[document_examples]
397	///
398	/// ```
399	/// use fp_library::{
400	/// 	brands::*,
401	/// 	functions::*,
402	/// };
403	///
404	/// let x = Some(5);
405	/// let y = filter_map::<OptionBrand, _, _>(|a| if a > 2 { Some(a * 2) } else { None }, x);
406	/// assert_eq!(y, Some(10));
407	/// ```
408	pub fn filter_map<'a, Brand: Filterable, A: 'a, B: 'a>(
409		func: impl Fn(A) -> Option<B> + 'a,
410		fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
411	) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
412		Brand::filter_map(func, fa)
413	}
414
415	/// Filters a data structure based on a predicate.
416	///
417	/// Free function version that dispatches to [the type class' associated function][`Filterable::filter`].
418	#[document_signature]
419	///
420	#[document_type_parameters(
421		"The lifetime of the elements.",
422		"The brand of the filterable structure.",
423		"The type of the elements in the structure."
424	)]
425	///
426	#[document_parameters("The predicate function.", "The data structure to filter.")]
427	///
428	#[document_returns(
429		"A new data structure containing only the elements that satisfy the predicate."
430	)]
431	#[document_examples]
432	///
433	/// ```
434	/// use fp_library::{
435	/// 	brands::*,
436	/// 	functions::*,
437	/// };
438	///
439	/// let x = Some(5);
440	/// let y = filter::<OptionBrand, _>(|a| a > 2, x);
441	/// assert_eq!(y, Some(5));
442	/// ```
443	pub fn filter<'a, Brand: Filterable, A: 'a + Clone>(
444		func: impl Fn(A) -> bool + 'a,
445		fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
446	) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
447		Brand::filter(func, fa)
448	}
449}
450
451pub use inner::*;