Skip to main content

RefFoldable

Trait RefFoldable 

Source
pub trait RefFoldable: Kind_cdc7cd43dac7585f {
    // Provided methods
    fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
        func: impl Fn(&A) -> M + 'a,
        fa: &<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> M
       where FnBrand: LiftFn + 'a,
             M: Monoid + 'a { ... }
    fn ref_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: LiftFn + 'a { ... }
    fn ref_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: LiftFn + 'a { ... }
}
Expand description

By-reference folding over a structure.

Similar to Foldable, but closures receive &A instead of A. This is the honest interface for memoized types like Lazy that internally hold a cached &A and would otherwise force a clone to satisfy the by-value Foldable signature.

All three methods (ref_fold_map, ref_fold_right, ref_fold_left) have default implementations in terms of each other, so implementors only need to provide one.

Provided Methods§

Source

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

Maps values to a monoid by reference and combines them.

§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 monoid type.
§Parameters
  • func: The function to map each element reference to a monoid.
  • fa: The structure to fold.
§Returns

The combined monoid value.

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

let lazy = RcLazy::new(|| 5);
let result =
	fold_map::<RcFnBrand, LazyBrand<RcLazyConfig>, _, _, _, _>(|a: &i32| a.to_string(), &lazy);
assert_eq!(result, "5");
Source

fn ref_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: LiftFn + 'a,

Folds the structure from the right by reference.

§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 reference 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::explicit::*,
	types::*,
};

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

fn ref_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: LiftFn + 'a,

Folds the structure from the left by reference.

§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 reference.
  • initial: The initial value of the accumulator.
  • fa: The structure to fold.
§Returns

The final accumulator value.

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

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

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 RefFoldable for CatListBrand

Source§

impl RefFoldable for IdentityBrand

Source§

impl RefFoldable for OptionBrand

Source§

impl RefFoldable for Tuple1Brand

Source§

impl RefFoldable for VecBrand

Source§

impl<Config: LazyConfig> RefFoldable for LazyBrand<Config>

§Type Parameters
  • Config: The memoization configuration (determines Rc vs Arc).
Source§

impl<E: 'static, Config: LazyConfig> RefFoldable for TryLazyBrand<E, Config>

§Type Parameters
  • E: The error type.
  • Config: The memoization configuration.
Source§

impl<E: Clone + 'static> RefFoldable for ResultErrAppliedBrand<E>

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

impl<First: Clone + 'static> RefFoldable for PairFirstAppliedBrand<First>

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

impl<First: Clone + 'static> RefFoldable for Tuple2FirstAppliedBrand<First>

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

impl<Second: Clone + 'static> RefFoldable for PairSecondAppliedBrand<Second>

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

impl<Second: Clone + 'static> RefFoldable for Tuple2SecondAppliedBrand<Second>

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

impl<T: Clone + 'static> RefFoldable for ResultOkAppliedBrand<T>

§Type Parameters
  • T: The success type.