1#[fp_macros::document_module]
17mod inner {
18 use {
19 crate::{
20 classes::*,
21 kinds::*,
22 },
23 fp_macros::*,
24 };
25
26 pub trait Filterable: Compactable + Functor {
43 #[document_signature]
47 #[document_type_parameters(
49 "The lifetime of the elements.",
50 "The type of the elements in the input structure.",
51 "The type of the error values.",
52 "The type of the success values."
53 )]
54 #[document_parameters(
56 "The function to apply to each element, returning a [`Result`].",
57 "The data structure to partition."
58 )]
59 #[document_returns(
61 "A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
62 )]
63 #[document_examples]
64 fn partition_map<'a, A: 'a, E: 'a, O: 'a>(
78 func: impl Fn(A) -> Result<O, E> + 'a,
79 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
80 ) -> (
81 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
82 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
83 ) {
84 Self::separate::<E, O>(Self::map::<A, Result<O, E>>(func, fa))
85 }
86
87 #[document_signature]
95 #[document_type_parameters(
97 "The lifetime of the elements.",
98 "The type of the elements in the structure."
99 )]
100 #[document_parameters("The predicate function.", "The data structure to partition.")]
102 #[document_returns(
104 "A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
105 )]
106 #[document_examples]
107 fn partition<'a, A: 'a + Clone>(
120 func: impl Fn(A) -> bool + 'a,
121 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
122 ) -> (
123 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
124 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
125 ) {
126 Self::partition_map(move |a| if func(a.clone()) { Ok(a) } else { Err(a) }, fa)
127 }
128
129 #[document_signature]
135 #[document_type_parameters(
137 "The lifetime of the elements.",
138 "The type of the elements in the input structure.",
139 "The type of the elements in the output structure."
140 )]
141 #[document_parameters(
142 "The function to apply to each element, returning an [`Option`].",
143 "The data structure to filter and map."
144 )]
145 #[document_returns(
146 "A new data structure containing only the values where the function returned [`Some`]."
147 )]
148 #[document_examples]
149 fn filter_map<'a, A: 'a, B: 'a>(
161 func: impl Fn(A) -> Option<B> + 'a,
162 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
163 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
164 Self::compact::<B>(Self::map::<A, Option<B>>(func, fa))
165 }
166
167 #[document_signature]
171 #[document_type_parameters(
173 "The lifetime of the elements.",
174 "The type of the elements in the structure."
175 )]
176 #[document_parameters("The predicate function.", "The data structure to filter.")]
178 #[document_returns(
180 "A new data structure containing only the elements that satisfy the predicate."
181 )]
182 #[document_examples]
183 fn filter<'a, A: 'a + Clone>(
195 func: impl Fn(A) -> bool + 'a,
196 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
197 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
198 Self::filter_map(move |a| if func(a.clone()) { Some(a) } else { None }, fa)
199 }
200 }
201
202 #[document_signature]
206 #[document_type_parameters(
208 "The lifetime of the elements.",
209 "The brand of the filterable structure.",
210 "The type of the elements in the input structure.",
211 "The type of the error values.",
212 "The type of the success values."
213 )]
214 #[document_parameters(
216 "The function to apply to each element, returning a [`Result`].",
217 "The data structure to partition."
218 )]
219 #[document_returns(
221 "A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
222 )]
223 #[document_examples]
224 pub fn partition_map<'a, Brand: Filterable, A: 'a, E: 'a, O: 'a>(
238 func: impl Fn(A) -> Result<O, E> + 'a,
239 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
240 ) -> (
241 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
242 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
243 ) {
244 Brand::partition_map(func, fa)
245 }
246
247 #[document_signature]
253 #[document_type_parameters(
255 "The lifetime of the elements.",
256 "The brand of the filterable structure.",
257 "The type of the elements in the structure."
258 )]
259 #[document_parameters("The predicate function.", "The data structure to partition.")]
261 #[document_returns(
263 "A pair of data structures: the first containing elements that do not satisfy the predicate, and the second containing elements that do."
264 )]
265 #[document_examples]
266 pub fn partition<'a, Brand: Filterable, A: 'a + Clone>(
279 func: impl Fn(A) -> bool + 'a,
280 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
281 ) -> (
282 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
283 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
284 ) {
285 Brand::partition(func, fa)
286 }
287
288 #[document_signature]
292 #[document_type_parameters(
294 "The lifetime of the elements.",
295 "The brand of the filterable structure.",
296 "The type of the elements in the input structure.",
297 "The type of the elements in the output structure."
298 )]
299 #[document_parameters(
301 "The function to apply to each element, returning an [`Option`].",
302 "The data structure to filter and map."
303 )]
304 #[document_returns(
306 "A new data structure containing only the values where the function returned [`Some`]."
307 )]
308 #[document_examples]
309 pub fn filter_map<'a, Brand: Filterable, A: 'a, B: 'a>(
321 func: impl Fn(A) -> Option<B> + 'a,
322 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
323 ) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
324 Brand::filter_map(func, fa)
325 }
326
327 #[document_signature]
331 #[document_type_parameters(
333 "The lifetime of the elements.",
334 "The brand of the filterable structure.",
335 "The type of the elements in the structure."
336 )]
337 #[document_parameters("The predicate function.", "The data structure to filter.")]
339 #[document_returns(
341 "A new data structure containing only the elements that satisfy the predicate."
342 )]
343 #[document_examples]
344 pub fn filter<'a, Brand: Filterable, A: 'a + Clone>(
356 func: impl Fn(A) -> bool + 'a,
357 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
358 ) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
359 Brand::filter(func, fa)
360 }
361}
362
363pub use inner::*;