Skip to main content

partition

Function partition 

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

Partitions the values in a filterable context using a predicate.

Dispatches to either Filterable::partition or RefFilterable::ref_partition based on the closure’s argument type:

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

The Marker and FA type parameters are inferred automatically by the compiler from the closure’s argument type and the container argument. Callers write partition::<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, 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 for the first partition or false for the second.
  • fa: The filterable instance (owned for Val, borrowed for Ref).

§Returns

A tuple of two filterable instances: the first contains elements satisfying the predicate, the second contains the rest.

§Examples

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

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

// By-ref: dispatches to RefFilterable::ref_partition
let (no, yes) = partition::<OptionBrand, _, _, _>(|x: &i32| *x > 3, &Some(5));
assert_eq!(yes, Some(5));
assert_eq!(no, None);