Skip to main content

Foldable

Trait Foldable 

Source
pub trait Foldable: Kind_cdc7cd43dac7585f {
    // Provided methods
    fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
        func: impl Fn(A, B) -> B + 'a,
        initial: B,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> B
       where FnBrand: CloneableFn + 'a { ... }
    fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
        func: impl Fn(B, A) -> B + 'a,
        initial: B,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> B
       where FnBrand: CloneableFn + 'a { ... }
    fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
        func: impl Fn(A) -> M + 'a,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> M
       where M: Monoid + 'a,
             FnBrand: CloneableFn + 'a { ... }
}
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.

§Minimal Implementation

A minimal implementation of Foldable requires implementing either Foldable::fold_right or Foldable::fold_map.

Note that Foldable::fold_left is not sufficient on its own because the default implementations of Foldable::fold_right and Foldable::fold_map do not depend on it.

Provided Methods§

Source

fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>( func: impl Fn(A, B) -> B + 'a, initial: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
where FnBrand: CloneableFn + 'a,

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

This method performs a right-associative fold of the structure.

§Type Signature

forall A B. ((A, B) -> B, B, Self A) -> B

§Type Parameters
  • 'a: The lifetime of the elements.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • B: The type of the accumulator.
§Parameters
  • func: The function to apply to each element and the accumulator.
  • initial: The initial value of the accumulator.
  • fa: The structure to fold.
§Returns

The final accumulator value.

§Examples
use fp_library::{
	brands::*,
	functions::*,
};

let x = Some(5);
let y = fold_right::<RcFnBrand, OptionBrand, _, _>(|a, b| a + b, 10, x);
assert_eq!(y, 15);
Source

fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>( func: impl Fn(B, A) -> B + 'a, initial: B, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> B
where FnBrand: CloneableFn + 'a,

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

This method performs a left-associative fold of the structure.

§Type Signature

forall A B. ((B, A) -> B, B, Self A) -> B

§Type Parameters
  • 'a: The lifetime of the elements.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • B: The type of the accumulator.
§Parameters
  • func: The function to apply to the accumulator and each element.
  • initial: The initial value of the accumulator.
  • fa: The structure to fold.
§Returns

The final accumulator value.

§Examples
use fp_library::{
	brands::*,
	functions::*,
};

let x = Some(5);
let y = fold_left::<RcFnBrand, OptionBrand, _, _>(|b, a| b + a, 10, x);
assert_eq!(y, 15);
Source

fn fold_map<'a, FnBrand, A: 'a + Clone, M>( func: impl Fn(A) -> M + 'a, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> M
where M: Monoid + 'a, FnBrand: CloneableFn + 'a,

Maps values to a monoid and combines them.

This method maps each element of the structure to a monoid and then combines the results using the monoid’s append operation.

§Type Signature

forall A M. Monoid M => (A -> M, Self A) -> M

§Type Parameters
  • 'a: The lifetime of the elements.
  • FnBrand: The brand of the cloneable function to use.
  • A: The type of the elements in the structure.
  • M: The type of the monoid.
§Parameters
  • func: The function to map each element to a monoid.
  • fa: The structure to fold.
§Returns

The combined monoid value.

§Examples
use fp_library::{
	brands::*,
	functions::*,
};

let x = Some(5);
let y = fold_map::<RcFnBrand, OptionBrand, _, _>(|a: i32| a.to_string(), x);
assert_eq!(y, "5".to_string());

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.

Implementors§

Source§

impl Foldable for CatListBrand

Source§

impl Foldable for IdentityBrand

Source§

impl Foldable for OptionBrand

Source§

impl Foldable for ThunkBrand

Source§

impl Foldable for Tuple1Brand

Source§

impl Foldable for VecBrand

Source§

impl<A: 'static> Foldable for TryThunkOkAppliedBrand<A>

§Type Parameters
  • A: The success type.
Source§

impl<DoneType: 'static> Foldable for StepDoneAppliedBrand<DoneType>

§Type Parameters
  • DoneType: The done type.
Source§

impl<E: 'static> Foldable for ResultErrAppliedBrand<E>

§Type Parameters
  • E: The error type.
Source§

impl<E: 'static> Foldable for TryThunkErrAppliedBrand<E>

§Type Parameters
  • E: The error type.
Source§

impl<First: 'static> Foldable for PairFirstAppliedBrand<First>

§Type Parameters
  • First: The type of the first value in the pair.
Source§

impl<First: 'static> Foldable for Tuple2FirstAppliedBrand<First>

§Type Parameters
  • First: The type of the first value in the tuple.
Source§

impl<LoopType: 'static> Foldable for StepLoopAppliedBrand<LoopType>

§Type Parameters
  • LoopType: The loop type.
Source§

impl<Second: 'static> Foldable for PairSecondAppliedBrand<Second>

§Type Parameters
  • Second: The type of the second value in the pair.
Source§

impl<Second: 'static> Foldable for Tuple2SecondAppliedBrand<Second>

§Type Parameters
  • Second: The type of the second value in the tuple.
Source§

impl<T: 'static> Foldable for ResultOkAppliedBrand<T>

§Type Parameters
  • T: The success type.