Skip to main content

fold_map

Function fold_map 

Source
pub fn fold_map<'a, FnBrand, Brand: Foldable, A: 'a + Clone, M, Func>(
    func: Func,
    fa: <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> M
where M: Monoid + 'a, Func: Fn(A) -> M + 'a, FnBrand: CloneableFn + 'a,
Expand description

Maps values to a monoid and combines them.

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

§Type Signature

forall brand a m. (Foldable brand, Monoid m) => (a -> m, brand a) -> m

§Type Parameters

  • 'a: The lifetime of the elements.
  • FnBrand: The brand of the cloneable function to use.
  • Brand: The brand of the foldable structure.
  • A: The type of the elements in the structure.
  • M: The type of the monoid.
  • Func: The type of the mapping function.

§Parameters

  • func: The function to map each element to a monoid.
  • fa: The structure to fold.

§Returns

The combined monoid value.

§Examples

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

let x = Some(5);
let y = fold_map::<RcFnBrand, OptionBrand, _, _, _>(|a: i32| a.to_string(), x);
assert_eq!(y, "5".to_string());