pub fn filter_map<'a, Brand: Kind_cdc7cd43dac7585f, A: 'a, B: 'a, FA, Marker>(
f: impl FilterMapDispatch<'a, Brand, A, B, FA, Marker>,
fa: FA,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>Expand description
Filters and maps the values in a filterable context.
Dispatches to either Filterable::filter_map or
RefFilterable::ref_filter_map based on the closure’s argument type:
- If the closure takes owned values (
Fn(A) -> Option<B>) and the container is owned, dispatches toFilterable::filter_map. - If the closure takes references (
Fn(&A) -> Option<B>) and the container is borrowed (&fa), dispatches toRefFilterable::ref_filter_map.
The Marker and FA type parameters are inferred automatically by the
compiler from the closure’s argument type and the container argument.
Callers write filter_map::<Brand, _, _, _, _>(...) and never need to
specify Marker or FA explicitly.
The dispatch is resolved at compile time with no runtime cost.
§Type Signature
forall Brand A B. Filterable Brand => (A -> Option B, Brand A) -> Brand B
§Type Parameters
'a: The lifetime of the values.Brand: The brand of the filterable.A: The type of the value(s) inside the filterable.B: The type of the result(s) of applying the function.FA: The container type (owned or borrowed), inferred from the argument.Marker: Dispatch marker type, inferred automatically.
§Parameters
f: The function to apply to each value. ReturnsSome(b)to keep the value orNoneto discard it.fa: The filterable instance (owned for Val, borrowed for Ref).
§Returns
A new filterable instance containing only the values for which the function returned Some.
§Examples
use fp_library::{
brands::*,
functions::explicit::*,
};
// Owned: dispatches to Filterable::filter_map
let y =
filter_map::<OptionBrand, _, _, _, _>(|x: i32| if x > 3 { Some(x) } else { None }, Some(5));
assert_eq!(y, Some(5));
// By-ref: dispatches to RefFilterable::ref_filter_map
let y = filter_map::<OptionBrand, _, _, _, _>(
|x: &i32| if *x > 3 { Some(*x) } else { None },
&Some(5),
);
assert_eq!(y, Some(5));