pub trait RefBifoldable: Kind_266801a817966495 {
// Provided methods
fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(&A, C) -> C + 'a,
g: impl Fn(&B, C) -> C + 'a,
z: C,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> C { ... }
fn ref_bi_fold_left<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(C, &A) -> C + 'a,
g: impl Fn(C, &B) -> C + 'a,
z: C,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> C { ... }
fn ref_bi_fold_map<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
f: impl Fn(&A) -> M + 'a,
g: impl Fn(&B) -> M + 'a,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> M
where M: Monoid + 'a { ... }
}Expand description
By-reference folding over a bifoldable structure.
Similar to Bifoldable, but closures receive &A and &B instead of
owned values. This is the honest interface for types that internally hold
cached or borrowed values and would otherwise force a clone to satisfy the
by-value Bifoldable signature.
§Minimal Implementation
A minimal implementation requires either RefBifoldable::ref_bi_fold_map or
RefBifoldable::ref_bi_fold_right to be defined directly:
- If
RefBifoldable::ref_bi_fold_rightis implemented,RefBifoldable::ref_bi_fold_mapandRefBifoldable::ref_bi_fold_leftare derived from it. - If
RefBifoldable::ref_bi_fold_mapis implemented,RefBifoldable::ref_bi_fold_rightis derived viaEndofunction, andRefBifoldable::ref_bi_fold_leftis derived from the derivedRefBifoldable::ref_bi_fold_right.
Note: defining both defaults creates a circular dependency and will not terminate.
§Laws
RefBifoldable instances must be internally consistent and consistent with
Bifunctor when one is also defined:
- ref_bi_fold_map/ref_bi_fold_right consistency:
ref_bi_fold_map(f, g, &x) = ref_bi_fold_right(|a, c| append(f(a), c), |b, c| append(g(b), c), empty(), &x).
§Examples
RefBifoldable laws for Result:
use fp_library::{
brands::*,
functions::{
explicit::{
bi_fold_map,
bi_fold_right,
},
*,
},
};
// ResultBrand has Of<E, A> = Result<A, E>, so the first function handles errors
// and the second function handles ok values.
let ok: Result<i32, String> = Ok(5);
let err: Result<i32, String> = Err("err".to_string());
let f = |e: &String| format!("err:{e}");
let g = |a: &i32| a.to_string();
// bi_fold_map/bi_fold_right consistency with ref closures (Ok case):
assert_eq!(
bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>((f, g), &ok),
bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
(|a: &String, c: String| append(f(a), c), |b: &i32, c: String| append(g(b), c)),
empty::<String>(),
&ok,
),
);
// bi_fold_map/bi_fold_right consistency with ref closures (Err case):
assert_eq!(
bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>((f, g), &err),
bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
(|a: &String, c: String| append(f(a), c), |b: &i32, c: String| append(g(b), c)),
empty::<String>(),
&err,
),
);Provided Methods§
Sourcefn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(&A, C) -> C + 'a,
g: impl Fn(&B, C) -> C + 'a,
z: C,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> C
fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>( f: impl Fn(&A, C) -> C + 'a, g: impl Fn(&B, C) -> C + 'a, z: C, p: &<Self as Kind_266801a817966495>::Of<'a, A, B>, ) -> C
Folds the bifoldable structure from right to left by reference using two step functions.
This method performs a right-associative fold, dispatching to f for
elements of the first type and g for elements of the second type.
Closures receive references rather than owned values.
§Type Signature
forall A B C. ((&A, C) -> C, (&B, C) -> C, C, &Self A B) -> C
§Type Parameters
'a: The lifetime of the values.FnBrand: The brand of the cloneable function to use.A: The type of the first-position elements.B: The type of the second-position elements.C: The type of the accumulator.
§Parameters
f: The step function for first-position element references.g: The step function for second-position element references.z: The initial accumulator value.p: The bifoldable structure to fold (borrowed).
§Returns
The final accumulator value after folding all elements.
§Examples
use fp_library::{
brands::*,
functions::explicit::*,
};
let x: Result<i32, i32> = Err(3);
let y = bi_fold_right::<RcFnBrand, ResultBrand, _, _, _, _, _>(
(|e: &i32, acc| acc - *e, |s: &i32, acc| acc + *s),
10,
&x,
);
assert_eq!(y, 7);Sourcefn ref_bi_fold_left<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
f: impl Fn(C, &A) -> C + 'a,
g: impl Fn(C, &B) -> C + 'a,
z: C,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> C
fn ref_bi_fold_left<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>( f: impl Fn(C, &A) -> C + 'a, g: impl Fn(C, &B) -> C + 'a, z: C, p: &<Self as Kind_266801a817966495>::Of<'a, A, B>, ) -> C
Folds the bifoldable structure from left to right by reference using two step functions.
This method performs a left-associative fold, dispatching to f for
elements of the first type and g for elements of the second type.
Closures receive references rather than owned values.
§Type Signature
forall A B C. ((C, &A) -> C, (C, &B) -> C, C, &Self A B) -> C
§Type Parameters
'a: The lifetime of the values.FnBrand: The brand of the cloneable function to use.A: The type of the first-position elements.B: The type of the second-position elements.C: The type of the accumulator.
§Parameters
f: The step function for first-position element references.g: The step function for second-position element references.z: The initial accumulator value.p: The bifoldable structure to fold (borrowed).
§Returns
The final accumulator value after folding all elements.
§Examples
use fp_library::{
brands::*,
functions::explicit::*,
};
let x: Result<i32, i32> = Ok(5);
let y = bi_fold_left::<RcFnBrand, ResultBrand, _, _, _, _, _>(
(|acc, e: &i32| acc - *e, |acc, s: &i32| acc + *s),
10,
&x,
);
assert_eq!(y, 15);Sourcefn ref_bi_fold_map<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
f: impl Fn(&A) -> M + 'a,
g: impl Fn(&B) -> M + 'a,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> Mwhere
M: Monoid + 'a,
fn ref_bi_fold_map<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
f: impl Fn(&A) -> M + 'a,
g: impl Fn(&B) -> M + 'a,
p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
) -> Mwhere
M: Monoid + 'a,
Maps elements of both types to a monoid by reference and combines the results.
This method maps each element of the first type using f and each element
of the second type using g, receiving references rather than owned values,
then combines all results using the monoid’s append operation.
§Type Signature
forall A B M. Monoid M => (&A -> M, &B -> M, &Self A B) -> M
§Type Parameters
'a: The lifetime of the values.FnBrand: The brand of the cloneable function to use.A: The type of the first-position elements.B: The type of the second-position elements.M: The monoid type to fold into.
§Parameters
f: The function mapping first-position element references to the monoid.g: The function mapping second-position element references to the monoid.p: The bifoldable structure to fold (borrowed).
§Returns
The combined monoid value.
§Examples
use fp_library::{
brands::*,
functions::explicit::*,
};
let x: Result<i32, i32> = Ok(5);
let y = bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
(|e: &i32| e.to_string(), |s: &i32| s.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.