1#[fp_macros::document_module]
18mod inner {
19 use {
20 crate::{
21 classes::*,
22 kinds::*,
23 },
24 fp_macros::*,
25 };
26
27 #[kind(type Of<'a, A: 'a>: 'a;)]
39 pub trait RefFilterable: RefFunctor + Compactable {
40 #[document_signature]
42 #[document_type_parameters(
44 "The lifetime of the elements.",
45 "The type of the elements in the input structure.",
46 "The type of the error values.",
47 "The type of the success values."
48 )]
49 #[document_parameters(
51 "The function to apply to each element reference, returning a `Result`.",
52 "The structure to partition."
53 )]
54 #[document_returns("A pair of (errors, successes).")]
56 #[document_examples]
57 fn ref_partition_map<'a, A: 'a, E: 'a, O: 'a>(
73 func: impl Fn(&A) -> Result<O, E> + 'a,
74 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
75 ) -> (
76 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
77 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
78 ) {
79 Self::separate::<E, O>(Self::ref_map::<A, Result<O, E>>(func, fa))
80 }
81
82 #[document_signature]
86 #[document_type_parameters(
88 "The lifetime of the elements.",
89 "The type of the elements in the structure."
90 )]
91 #[document_parameters("The predicate function.", "The structure to partition.")]
93 #[document_returns("A pair of (not satisfied, satisfied).")]
95 #[document_examples]
96 fn ref_partition<'a, A: 'a + Clone>(
109 func: impl Fn(&A) -> bool + 'a,
110 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
111 ) -> (
112 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
113 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
114 ) {
115 Self::ref_partition_map(
116 move |a: &A| {
117 if func(a) { Ok(a.clone()) } else { Err(a.clone()) }
118 },
119 fa,
120 )
121 }
122
123 #[document_signature]
125 #[document_type_parameters(
127 "The lifetime of the elements.",
128 "The type of the elements in the input structure.",
129 "The type of the elements in the output structure."
130 )]
131 #[document_parameters(
133 "The function to apply to each element reference, returning an `Option`.",
134 "The structure to filter and map."
135 )]
136 #[document_returns("The structure with `None` results removed.")]
138 #[document_examples]
139 fn ref_filter_map<'a, A: 'a, B: 'a>(
152 func: impl Fn(&A) -> Option<B> + 'a,
153 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
154 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
155 Self::compact::<B>(Self::ref_map::<A, Option<B>>(func, fa))
156 }
157
158 #[document_signature]
160 #[document_type_parameters(
162 "The lifetime of the elements.",
163 "The type of the elements in the structure."
164 )]
165 #[document_parameters("The predicate function.", "The structure to filter.")]
167 #[document_returns("The structure with elements not satisfying the predicate removed.")]
169 #[document_examples]
170 fn ref_filter<'a, A: 'a + Clone>(
182 func: impl Fn(&A) -> bool + 'a,
183 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
184 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
185 Self::ref_filter_map(move |a: &A| if func(a) { Some(a.clone()) } else { None }, fa)
186 }
187 }
188
189 #[document_signature]
193 #[document_type_parameters(
195 "The lifetime of the elements.",
196 "The brand of the structure.",
197 "The type of the elements.",
198 "The error type.",
199 "The success type."
200 )]
201 #[document_parameters("The partitioning function.", "The structure to partition.")]
203 #[document_returns("A pair of (errors, successes).")]
205 #[document_examples]
206 pub fn ref_partition_map<'a, Brand: RefFilterable, A: 'a, E: 'a, O: 'a>(
222 func: impl Fn(&A) -> Result<O, E> + 'a,
223 fa: &Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
224 ) -> (
225 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
226 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
227 ) {
228 Brand::ref_partition_map(func, fa)
229 }
230
231 #[document_signature]
235 #[document_type_parameters(
237 "The lifetime of the elements.",
238 "The brand of the structure.",
239 "The type of the elements."
240 )]
241 #[document_parameters("The predicate.", "The structure to partition.")]
243 #[document_returns("A pair of (not satisfied, satisfied).")]
245 #[document_examples]
246 pub fn ref_partition<'a, Brand: RefFilterable, A: 'a + Clone>(
259 func: impl Fn(&A) -> bool + 'a,
260 fa: &Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
261 ) -> (
262 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
263 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
264 ) {
265 Brand::ref_partition(func, fa)
266 }
267
268 #[document_signature]
272 #[document_type_parameters(
274 "The lifetime of the elements.",
275 "The brand of the structure.",
276 "The type of the input elements.",
277 "The type of the output elements."
278 )]
279 #[document_parameters("The filter-map function.", "The structure to filter.")]
281 #[document_returns("The filtered structure.")]
283 #[document_examples]
284 pub fn ref_filter_map<'a, Brand: RefFilterable, A: 'a, B: 'a>(
297 func: impl Fn(&A) -> Option<B> + 'a,
298 fa: &Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
299 ) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
300 Brand::ref_filter_map(func, fa)
301 }
302
303 #[document_signature]
307 #[document_type_parameters(
309 "The lifetime of the elements.",
310 "The brand of the structure.",
311 "The type of the elements."
312 )]
313 #[document_parameters("The predicate.", "The structure to filter.")]
315 #[document_returns("The filtered structure.")]
317 #[document_examples]
318 pub fn ref_filter<'a, Brand: RefFilterable, A: 'a + Clone>(
330 func: impl Fn(&A) -> bool + 'a,
331 fa: &Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
332 ) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
333 Brand::ref_filter(func, fa)
334 }
335}
336
337pub use inner::*;