pub trait Foldable: Kind0L1T {
// Provided methods
fn fold_left<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>(
f: ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, A, B>>,
) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>> { ... }
fn fold_map<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, M: Monoid<'a> + Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, M>,
) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, M> { ... }
fn fold_right<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, ApplyFn<'a, ClonableFnBrand, B, B>>,
) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>> { ... }
}Expand description
A type class for structures that can be folded to a single value.
A Foldable represents a structure that can be folded over to combine its elements
into a single result. This is useful for operations like summing values, collecting into a collection,
or applying monoidal operations.
A minimum implementation of Foldable requires the manual implementation of at least Foldable::fold_right or Foldable::fold_map.
Provided Methods§
Sourcefn fold_left<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>(
f: ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, A, B>>,
) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>>
fn fold_left<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>( f: ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, A, B>>, ) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>>
Folds the structure by applying a function from left to right.
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 typeB.fa: A foldable structure containing values of typeA.
§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
);Sourcefn fold_map<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, M: Monoid<'a> + Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, M>,
) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, M>
fn fold_map<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, M: Monoid<'a> + Clone>( f: ApplyFn<'a, ClonableFnBrand, A, M>, ) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, M>
Maps values to a monoid and combines them.
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 a. 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 typeA.
§Returns
Final monoid obtained from the folding operation.
§Examples
use fp_library::{brands::{VecBrand, RcFnBrand}, functions::{fold_map, identity}};
use std::rc::Rc;
assert_eq!(
fold_map::<RcFnBrand, VecBrand, _, String>(Rc::new(identity))(vec![
"Hello, ".to_string(),
"World!".to_string()
]),
"Hello, World!"
);Sourcefn fold_right<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, ApplyFn<'a, ClonableFnBrand, B, B>>,
) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>>
fn fold_right<'a, ClonableFnBrand: 'a + ClonableFn, A: Clone, B: Clone>( f: ApplyFn<'a, ClonableFnBrand, A, ApplyFn<'a, ClonableFnBrand, B, B>>, ) -> ApplyFn<'a, ClonableFnBrand, B, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, B>>
Folds the structure by applying a function from right to left.
The default implementation of fold_right is implemented in terms of fold_map using the Endofunction 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 typeB.fa: A foldable structure containing values of typeA.
§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
);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.