Skip to main content

bimap

Function bimap 

Source
pub fn bimap<'a, Brand: Kind_266801a817966495, A: 'a, B: 'a, C: 'a, D: 'a, FA, Marker>(
    fg: impl BimapDispatch<'a, Brand, A, B, C, D, FA, Marker>,
    p: FA,
) -> <Brand as Kind_266801a817966495>::Of<'a, B, D>
Expand description

Maps two functions over the values in a bifunctor context.

Dispatches to either Bifunctor::bimap or RefBifunctor::ref_bimap based on the closures’ argument types.

The Marker and FA type parameters are inferred automatically by the compiler from the closures’ argument types and the container argument. Callers write bimap::<Brand, _, _, _, _, _, _>(...) and never need to specify Marker or FA explicitly.

§Type Signature

forall Brand A B C D. Bifunctor Brand => ((A -> B, C -> D), Brand A C) -> Brand B D

§Type Parameters

  • 'a: The lifetime of the values.
  • Brand: The brand of the bifunctor.
  • A: The type of the first value.
  • B: The type of the first result.
  • C: The type of the second value.
  • D: The type of the second result.
  • FA: The container type (owned or borrowed), inferred from the argument.
  • Marker: Dispatch marker type, inferred automatically.

§Parameters

  • fg: A tuple of (first function, second function).
  • p: The bifunctor value (owned for Val, borrowed for Ref).

§Returns

A new bifunctor instance containing the results of applying the functions.

§Examples

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

// Owned
let x = Result::<i32, i32>::Ok(5);
let y = bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x);
assert_eq!(y, Ok(10));

// By-ref
let x = Result::<i32, i32>::Ok(5);
let y = bimap::<ResultBrand, _, _, _, _, _, _>((|e: &i32| *e + 1, |s: &i32| *s * 2), &x);
assert_eq!(y, Ok(10));