Skip to main content

filter_map

Function filter_map 

Source
pub fn filter_map<'a, FA, A: 'a, B: 'a, Brand>(
    f: impl FilterMapDispatch<'a, Brand, A, B, FA, <FA as InferableBrand_cdc7cd43dac7585f<'a, Brand, A>>::Marker>,
    fa: FA,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>
Expand description

Filters and maps the values in a filterable context, inferring the brand from the container type.

The Brand type parameter is inferred from the concrete type of fa via the InferableBrand trait. Both owned and borrowed containers are supported.

For types with multiple brands, use explicit::filter_map with a turbofish.

§Type Signature

forall Brand A B. Filterable Brand => (A -> Option B, Brand A) -> Brand B

§Type Parameters

  • 'a: The lifetime of the values.
  • FA: The container type (owned or borrowed). Brand is inferred from this.
  • A: The type of the value(s) inside the filterable.
  • B: The type of the result(s) of applying the function.
  • Brand: The brand, inferred via InferableBrand from FA and the element type.

§Parameters

  • f: The function to apply to each value. Returns Some(b) to keep or None to discard.
  • fa: The filterable instance (owned or borrowed).

§Returns

A new filterable instance containing only the kept values.

§Examples

use fp_library::functions::*;

let y = filter_map(|x: i32| if x > 3 { Some(x) } else { None }, Some(5));
assert_eq!(y, Some(5));