Function fold_left

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

Folds the structure by applying a function from left to right.

Free function version that dispatches to the typeclass method.

The default implementation of fold_left is implemented in terms of fold_right, flip, compose and identity where:

((fold_left f) b) fa = (((fold_right (((compose (flip compose)) (flip f)))) identity) fa) b

§Type Signature

forall f a b. Foldable f => (b -> a -> b) -> b -> f a -> b

§Parameters

  • f: A curried binary function that takes in the current value of the accumulator, the next item in the structure and returns the next value of accumulator.
  • b: Initial value of type B.
  • fa: A foldable structure containing values of type A.

§Returns

Final value of type B obtained from the folding operation.

§Examples

use fp_library::{brands::VecBrand, functions::fold_left};
use std::sync::Arc;

assert_eq!(
    fold_left::<VecBrand, _, _>(Arc::new(|carry| Arc::new(move |item| carry * 2 + item)))(0)(
        vec![1, 2, 3]
    ),
    11
);