filter_map

Function filter_map 

Source
pub fn filter_map<'a, Brand: Filterable, B: 'a, A: 'a, Func>(
    func: Func,
    fa: <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>
where Func: Fn(A) -> Option<B> + 'a,
Expand description

Maps a function over a data structure and filters out None results.

Free function version that dispatches to the type class’ associated function.

§Type Signature

forall f b a. Filterable f => (a -> Option b, f a) -> f b

§Type Parameters

  • Brand: The brand of the filterable structure.
  • B: The type of the elements in the output structure.
  • A: The type of the elements in the input structure.
  • Func: The type of the function to apply.

§Parameters

  • func: The function to apply to each element, returning an Option.
  • fa: The data structure to filter and map.

§Returns

A new data structure containing only the values where the function returned Some.

§Examples

use fp_library::{brands::*, functions::*};

let x = Some(5);
let y = filter_map::<OptionBrand, _, _, _>(|a| if a > 2 { Some(a * 2) } else { None }, x);
assert_eq!(y, Some(10));