Skip to main content

RefBitraversable

Trait RefBitraversable 

Source
pub trait RefBitraversable:
    RefBifunctor
    + RefBifoldable
    + Kind_266801a817966495 {
    // Required method
    fn ref_bi_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, C: 'a + Clone, D: 'a + Clone, F: Applicative>(
        f: impl Fn(&A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, C> + 'a,
        g: impl Fn(&B) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, D> + 'a,
        p: &<Self as Kind_266801a817966495>::Of<'a, A, B>,
    ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_266801a817966495>::Of<'a, C, D>>
       where FnBrand: LiftFn + 'a,
             <Self as Kind_266801a817966495>::Of<'a, C, D>: Clone,
             <F as Kind_cdc7cd43dac7585f>::Of<'a, C>: Clone,
             <F as Kind_cdc7cd43dac7585f>::Of<'a, D>: Clone;

    // Provided method
    fn ref_bi_sequence<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
        ta: &<Self as Kind_266801a817966495>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>, <F as Kind_cdc7cd43dac7585f>::Of<'a, B>>,
    ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_266801a817966495>::Of<'a, A, B>>
       where FnBrand: LiftFn + 'a,
             <Self as Kind_266801a817966495>::Of<'a, A, B>: Clone,
             <F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
             <F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone { ... }
}
Expand description

A type class for data structures with two type arguments that can be traversed by reference.

Similar to Bitraversable, but closures receive &A and &B instead of owned values. This enables traversing bifunctor structures by reference without consuming elements.

§Minimal Implementation

A minimal implementation requires RefBitraversable::ref_bi_traverse to be defined directly. RefBitraversable::ref_bi_sequence is derived from it via Clone::clone.

Note: defining both defaults creates a circular dependency and will not terminate.

§Laws

RefBitraversable instances must be consistent with RefBifunctor and RefBifoldable:

  • Traverse/sequence consistency: ref_bi_traverse(f, g, x) = ref_bi_sequence(ref_bimap(f, g, x)).

§Examples

RefBitraversable laws for Result:

use fp_library::{
	brands::*,
	functions::{
		explicit::{
			bi_traverse,
			bimap,
		},
		*,
	},
};

// ResultBrand has Of<E, A> = Result<A, E>, so the first function handles errors
// and the second function handles ok values.
let f = |e: &String| if e.is_empty() { None } else { Some(e.len()) };
let g = |a: &i32| if *a > 0 { Some(a * 2) } else { None };

// Traverse/sequence consistency (Ok case):
// bi_traverse((f, g), &x) = ref_bi_sequence(&bimap((f, g), &x))
let ok: Result<i32, String> = Ok(5);
assert_eq!(
	bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>((&f, &g), &ok),
	ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&bimap::<
		ResultBrand,
		_,
		_,
		_,
		_,
		_,
		_,
	>((&f, &g), &ok),),
);

// Traverse/sequence consistency (Err case):
let err: Result<i32, String> = Err("hello".to_string());
assert_eq!(
	bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>((&f, &g), &err),
	ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&bimap::<
		ResultBrand,
		_,
		_,
		_,
		_,
		_,
		_,
	>((&f, &g), &err),),
);

Required Methods§

Source

fn ref_bi_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, C: 'a + Clone, D: 'a + Clone, F: Applicative>( f: impl Fn(&A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, C> + 'a, g: impl Fn(&B) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, D> + 'a, p: &<Self as Kind_266801a817966495>::Of<'a, A, B>, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_266801a817966495>::Of<'a, C, D>>
where FnBrand: LiftFn + 'a, <Self as Kind_266801a817966495>::Of<'a, C, D>: Clone, <F as Kind_cdc7cd43dac7585f>::Of<'a, C>: Clone, <F as Kind_cdc7cd43dac7585f>::Of<'a, D>: Clone,

Traverses the bitraversable structure by reference with two effectful functions.

This method applies f to references of first-position values and g to references of second-position values, accumulating all effects in the applicative context F.

§Type Signature

forall A B C D F. (Applicative F, LiftFn FnBrand) => (&A -> F C, &B -> F D, &Self A B) -> F (Self C D)

§Type Parameters
  • 'a: The lifetime of the values.
  • FnBrand: The brand of the cloneable function wrapper.
  • A: The type of the first-position elements.
  • B: The type of the second-position elements.
  • C: The output type for first-position elements.
  • D: The output type for second-position elements.
  • F: The applicative context.
§Parameters
  • f: The function for first-position element references.
  • g: The function for second-position element references.
  • p: The bitraversable structure to traverse by reference.
§Returns

The transformed structure wrapped in the applicative context.

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

let x: Result<i32, i32> = Err(3);
let y = bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
	(|e: &i32| Some(e + 1), |s: &i32| Some(s * 2)),
	&x,
);
assert_eq!(y, Some(Err(4)));

Provided Methods§

Source

fn ref_bi_sequence<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>( ta: &<Self as Kind_266801a817966495>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>, <F as Kind_cdc7cd43dac7585f>::Of<'a, B>>, ) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_266801a817966495>::Of<'a, A, B>>
where FnBrand: LiftFn + 'a, <Self as Kind_266801a817966495>::Of<'a, A, B>: Clone, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone, <F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,

Sequences a bitraversable structure containing applicative values by reference.

Collapses a structure of effectful values into a single effectful structure, cloning each element via Clone::clone through RefBitraversable::ref_bi_traverse.

§Type Signature

forall A B F. (Applicative F, LiftFn FnBrand) => &Self (F A) (F B) -> F (Self A B)

§Type Parameters
  • 'a: The lifetime of the values.
  • FnBrand: The brand of the cloneable function wrapper.
  • A: The type of the first-position elements.
  • B: The type of the second-position elements.
  • F: The applicative context.
§Parameters
  • ta: The bitraversable structure containing applicative values.
§Returns

The applicative context wrapping the bitraversable structure.

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

let x: Result<Option<i32>, Option<i32>> = Ok(Some(5));
let y = ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&x);
assert_eq!(y, Some(Ok(5)));

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§