Skip to main content

fold_map

Function fold_map 

Source
pub fn fold_map<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, A: 'a, M: Monoid + 'a, FA, Marker>(
    func: impl FoldMapDispatch<'a, FnBrand, Brand, A, M, FA, Marker>,
    fa: FA,
) -> M
Expand description

Maps values to a monoid and combines them.

Dispatches to Foldable::fold_map or RefFoldable::ref_fold_map based on whether the closure takes A or &A.

The Marker and FA type parameters are inferred automatically by the compiler from the closure’s argument type and the container argument.

§Type Signature

forall Brand A M. (Foldable Brand, Monoid M) => (A -> M, Brand A) -> M

§Type Parameters

  • 'a: The lifetime of the values.
  • FnBrand: The brand of the cloneable function to use.
  • Brand: The brand of the foldable structure.
  • A: The type of the elements.
  • M: The monoid type.
  • FA: The container type (owned or borrowed), inferred from the argument.
  • Marker: Dispatch marker type, inferred automatically.

§Parameters

  • func: The mapping function.
  • fa: The structure to fold (owned for Val, borrowed for Ref).

§Returns

The combined monoid value.

§Examples

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

// By-value
let result = fold_map::<RcFnBrand, VecBrand, _, _, _, _>(|a: i32| a.to_string(), vec![1, 2, 3]);
assert_eq!(result, "123");

// By-ref
let lazy = RcLazy::new(|| 10);
let result =
	fold_map::<RcFnBrand, LazyBrand<RcLazyConfig>, _, _, _, _>(|a: &i32| a.to_string(), &lazy);
assert_eq!(result, "10");