Skip to main content

fold_left

Function fold_left 

Source
pub fn fold_left<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, A: 'a + Clone, B: 'a, FA, Marker>(
    func: impl FoldLeftDispatch<'a, FnBrand, Brand, A, B, FA, Marker>,
    initial: B,
    fa: FA,
) -> B
Expand description

Folds a structure from the left.

Dispatches to Foldable::fold_left or RefFoldable::ref_fold_left 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 B. Foldable Brand => ((B, A) -> B, B, Brand A) -> B

§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.
  • B: The type of the accumulator.
  • FA: The container type (owned or borrowed), inferred from the argument.
  • Marker: Dispatch marker type, inferred automatically.

§Parameters

  • func: The folding function.
  • initial: The initial accumulator value.
  • fa: The structure to fold (owned for Val, borrowed for Ref).

§Returns

The final accumulator value.

§Examples

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

// By-value
let result = fold_left::<RcFnBrand, VecBrand, _, _, _, _>(|b, a| b + a, 0, vec![1, 2, 3]);
assert_eq!(result, 6);

// By-ref
let lazy = RcLazy::new(|| 10);
let result =
	fold_left::<RcFnBrand, LazyBrand<RcLazyConfig>, _, _, _, _>(|b, a: &i32| b + *a, 5, &lazy);
assert_eq!(result, 15);