Skip to main content

wither

Function wither 

Source
pub fn wither<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, M: Kind_cdc7cd43dac7585f, A: 'a, B: 'a, FA, Marker>(
    func: impl WitherDispatch<'a, FnBrand, Brand, M, A, B, FA, Marker>,
    ta: FA,
) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>>
Expand description

Maps a function over a data structure and filters out None results in an applicative context.

Dispatches to either Witherable::wither or RefWitherable::ref_wither based on the closure’s argument type.

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

§Type Signature

forall Brand M A B. (Witherable Brand, Applicative M) => (A -> M (Option B), Brand A) -> M (Brand B)

§Type Parameters

  • 'a: The lifetime of the values.
  • FnBrand: The brand of the cloneable function to use.
  • Brand: The brand of the witherable structure.
  • M: The applicative functor brand for the computation.
  • A: The type of the elements in the input structure.
  • B: The type of the elements in the output structure.
  • FA: The container type (owned or borrowed), inferred from the argument.
  • Marker: Dispatch marker type, inferred automatically.

§Parameters

  • func: The function to apply to each element, returning an Option in an applicative context.
  • ta: The witherable structure (owned for Val, borrowed for Ref).

§Returns

The filtered structure wrapped in the applicative context.

§Examples

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

let y = wither::<RcFnBrand, OptionBrand, OptionBrand, _, _, _, _>(
	|a: i32| Some(if a > 2 { Some(a * 2) } else { None }),
	Some(5),
);
assert_eq!(y, Some(Some(10)));