Skip to main content

partition_map

Function partition_map 

Source
pub fn partition_map<'a, Brand: Kind_cdc7cd43dac7585f, A: 'a, E: 'a, O: 'a, FA, Marker>(
    f: impl PartitionMapDispatch<'a, Brand, A, E, O, FA, Marker>,
    fa: FA,
) -> (<Brand as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Brand as Kind_cdc7cd43dac7585f>::Of<'a, O>)
Expand description

Partitions the values in a filterable context using a function that returns Result.

Dispatches to either Filterable::partition_map or RefFilterable::ref_partition_map based on the closure’s argument type:

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_map::<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 E O. Filterable Brand => (A -> Result O E, Brand A) -> (Brand E, Brand O)

§Type Parameters

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

§Parameters

  • f: The function to apply to each value. Returns Ok(o) for the success partition or Err(e) for the error partition.
  • fa: The filterable instance (owned for Val, borrowed for Ref).

§Returns

A tuple of two filterable instances: the first contains the Err values, the second contains the Ok values.

§Examples

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

// Owned: dispatches to Filterable::partition_map
let (errs, oks) =
	partition_map::<OptionBrand, _, _, _, _, _>(|x: i32| Ok::<i32, i32>(x * 2), Some(5));
assert_eq!(errs, None);
assert_eq!(oks, Some(10));

// By-ref: dispatches to RefFilterable::ref_partition_map
let (errs, oks) =
	partition_map::<OptionBrand, _, _, _, _, _>(|x: &i32| Ok::<i32, i32>(*x * 2), &Some(5));
assert_eq!(errs, None);
assert_eq!(oks, Some(10));