Skip to main content

apply_first

Function apply_first 

Source
pub fn apply_first<'a, Brand: Kind_cdc7cd43dac7585f, A: 'a, B: 'a, FA, Marker>(
    fa: FA,
    fb: <FA as ApplyFirstDispatch<'a, Brand, A, B, Marker>>::FB,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>
where FA: ApplyFirstDispatch<'a, Brand, A, B, Marker>,
Expand description

Sequences two applicative actions, keeping the result of the first.

Dispatches to either ApplyFirst::apply_first or RefApplyFirst::ref_apply_first based on whether the containers are owned or borrowed.

The Marker type parameter is inferred automatically by the compiler from the container arguments. Callers write apply_first::<Brand, _, _>(...) and never need to specify Marker explicitly.

The dispatch is resolved at compile time with no runtime cost.

§Type Signature

forall Brand A B. ApplyFirst Brand => (Brand A, Brand B) -> Brand A

§Type Parameters

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

§Parameters

  • fa: The first container (its values are preserved).
  • fb: The second container (its values are discarded).

§Returns

A container preserving the values from the first input.

§Examples

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

// Owned: dispatches to ApplyFirst::apply_first
let y = apply_first::<OptionBrand, _, _, _, _>(Some(5), Some(10));
assert_eq!(y, Some(5));

// By-ref: dispatches to RefApplyFirst::ref_apply_first
let a = Some(5);
let b = Some(10);
let y = apply_first::<OptionBrand, _, _, _, _>(&a, &b);
assert_eq!(y, Some(5));