fold_left

Function fold_left 

Source
pub fn fold_left<'a, ClonableFnBrand: 'a + ClonableFn, Brand: Foldable, A: Clone, B: Clone>(
    f: ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, A, B>>,
) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Brand, A>, B>>
Expand description

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

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

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 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, RcFnBrand}, functions::fold_left};
use std::rc::Rc;

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