Skip to main content

filter

Function filter 

Source
pub fn filter<'a, Brand: Kind_cdc7cd43dac7585f, A: 'a + Clone, FA, Marker>(
    f: impl FilterDispatch<'a, Brand, A, FA, Marker>,
    fa: FA,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, A>
Expand description

Filters the values in a filterable context using a predicate.

Dispatches to either Filterable::filter or RefFilterable::ref_filter based on the closure’s argument type:

  • If the closure takes owned values (Fn(A) -> bool) and the container is owned, dispatches to Filterable::filter.
  • If the closure takes references (Fn(&A) -> bool) and the container is borrowed (&fa), dispatches to RefFilterable::ref_filter.

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

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

§Type Signature

forall Brand A. Filterable Brand => (A -> bool, Brand A) -> Brand A

§Type Parameters

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

§Parameters

  • f: The predicate to apply to each value. Returns true to keep the value or false to discard it.
  • fa: The filterable instance (owned for Val, borrowed for Ref).

§Returns

A new filterable instance containing only the values for which the predicate returned true.

§Examples

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

// Owned: dispatches to Filterable::filter
let y = filter::<OptionBrand, _, _, _>(|x: i32| x > 3, Some(5));
assert_eq!(y, Some(5));

// By-ref: dispatches to RefFilterable::ref_filter
let y = filter::<OptionBrand, _, _, _>(|x: &i32| *x > 3, &Some(5));
assert_eq!(y, Some(5));