fold_right

Function fold_right 

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

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

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

The default implementation of fold_right is implemented in terms of fold_map using the Endomorphism monoid where:

((fold_right f) b) fa = ((fold_map f) fa) b

§Type Signature

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

§Parameters

  • f: A curried binary function that takes in the next item in the structure, the current value of the accumulator 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_right};
use std::rc::Rc;

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