Function fold_map

Source
pub fn fold_map<'a, Brand, A, M>(
    f: ClonableFn<'a, A, Apply<M, ()>>,
) -> ClonableFn<'a, Apply<Brand, (A,)>, Apply<M, ()>>
where Brand: Kind<(A,)> + Foldable, A: 'a + Clone, M: Monoid, Apply<M, ()>: 'a + Clone,
Expand description

Maps values to a monoid and combines them.

Free function version that dispatches to the typeclass method.

The default implementation of fold_map is implemented in terms of fold_right, compose, append and empty where:

fold_map f = (fold_right ((compose append) f)) empty

§Type Signature

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

§Parameters

  • f: A function that converts from values into monoidal elements.
  • fa: A foldable structure containing values of type A.

§Returns

Final monoid obtained from the folding operation.

§Examples

use fp_library::{brands::{StringBrand, VecBrand}, functions::{fold_map, identity}};
use std::sync::Arc;

assert_eq!(
    fold_map::<VecBrand, _, StringBrand>(Arc::new(identity))(vec![
        "Hello, ".to_string(),
        "World!".to_string()
    ]),
    "Hello, World!"
);