fold_map

Function fold_map 

Source
pub fn fold_map<'a, FnBrand, Brand: Foldable, Func, A: 'a + Clone, M>(
    func: Func,
    fa: <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> M
where M: Monoid + 'a, Func: Fn(A) -> M + 'a, FnBrand: ClonableFn + '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 a m. (Foldable f, Monoid m) => ((a) -> m, f a) -> m

§Type Parameters

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

§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::classes::foldable::fold_map;
use fp_library::brands::OptionBrand;
use fp_library::types::string; // Import Monoid impl for String
use fp_library::brands::RcFnBrand;

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