Skip to main content

Filterable

Trait Filterable 

Source
pub trait Filterable: Compactable + Functor {
    // Provided methods
    fn partition_map<'a, A: 'a, O: 'a, E: 'a, Func>(
        func: Func,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
       where Func: Fn(A) -> Result<O, E> + 'a { ... }
    fn partition<'a, A: 'a + Clone, Func>(
        func: Func,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>
       where Func: Fn(A) -> bool + 'a { ... }
    fn filter_map<'a, A: 'a, B: 'a, Func>(
        func: Func,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
       where Func: Fn(A) -> Option<B> + 'a { ... }
    fn filter<'a, A: 'a + Clone, Func>(
        func: Func,
        fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
       where Func: Fn(A) -> bool + 'a { ... }
}
Expand description

A type class for data structures that can be filtered and partitioned.

Filterable extends Compactable and Functor, adding methods for:

  • filter: Keeping elements that satisfy a predicate.
  • filter_map: Mapping and filtering in one step.
  • partition: Splitting elements based on a predicate.
  • partition_map: Mapping and partitioning in one step.

§Minimal Implementation

A minimal implementation of Filterable requires no specific method implementations, as all methods have default implementations based on Compactable and Functor.

However, it is recommended to implement Filterable::partition_map and Filterable::filter_map to avoid the intermediate structure created by the default implementations (which use map followed by separate or compact).

Provided Methods§

Source

fn partition_map<'a, A: 'a, O: 'a, E: 'a, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>
where Func: Fn(A) -> Result<O, E> + 'a,

Partitions a data structure based on a function that returns a Result.

The default implementation uses map and separate.

§Type Signature

forall self a o e. Filterable self => (a -> Result o e, self a) -> Pair (self o) (self e)

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The type of the elements in the input structure.
  • O: The type of the success values.
  • E: The type of the error values.
  • Func: The type of the function to apply.
§Parameters
  • func: The function to apply to each element, returning a Result.
  • fa: The data structure to partition.
§Returns

A pair of data structures: the first containing the Ok values, and the second containing the Err values.

§Examples
use fp_library::{brands::*, functions::*, types::*};

let x = Some(5);
let Pair(oks, errs) = partition_map::<OptionBrand, _, _, _, _>(|a| if a > 2 { Ok(a) } else { Err(a) }, x);
assert_eq!(oks, Some(5));
assert_eq!(errs, None);
Source

fn partition<'a, A: 'a + Clone, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>
where Func: Fn(A) -> bool + 'a,

Partitions a data structure based on a predicate.

The default implementation uses partition_map.

Note: The return order is (satisfied, not_satisfied), matching Rust’s Iterator::partition. This is achieved by mapping satisfied elements to Ok and unsatisfied elements to Err internally, as separate returns (Oks, Errs).

§Type Signature

forall self a. Filterable self => (a -> bool, self a) -> Pair (self a) (self a)

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The type of the elements in the structure.
  • Func: The type of the predicate function.
§Parameters
  • func: The predicate function.
  • fa: The data structure to partition.
§Returns

A pair of data structures: the first containing elements that satisfy the predicate, and the second containing elements that do not.

§Examples
use fp_library::{brands::*, functions::*, types::*};

let x = Some(5);
let Pair(satisfied, not_satisfied) = partition::<OptionBrand, _, _>(|a| a > 2, x);
assert_eq!(satisfied, Some(5));
assert_eq!(not_satisfied, None);
Source

fn filter_map<'a, A: 'a, B: 'a, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
where Func: Fn(A) -> Option<B> + 'a,

Maps a function over a data structure and filters out None results.

The default implementation uses map and compact.

§Type Signature
§Type Signature

forall self a b. Filterable self => (a -> Option b, self a) -> self b

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The type of the elements in the input structure.
  • B: The type of the elements in the output structure.
  • Func: The type of the function to apply.
§Parameters
  • func: The function to apply to each element, returning an Option.
  • fa: The data structure to filter and map.
§Returns

A new data structure containing only the values where the function returned Some.

§Examples
use fp_library::{brands::*, functions::*};

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

fn filter<'a, A: 'a + Clone, Func>( func: Func, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
where Func: Fn(A) -> bool + 'a,

Filters a data structure based on a predicate.

The default implementation uses filter_map.

§Type Signature

forall self a. Filterable self => (a -> bool, self a) -> self a

§Type Parameters
  • 'a: The lifetime of the elements.
  • A: The type of the elements in the structure.
  • Func: The type of the predicate function.
§Parameters
  • func: The predicate function.
  • fa: The data structure to filter.
§Returns

A new data structure containing only the elements that satisfy the predicate.

§Examples
use fp_library::{brands::*, functions::*};

let x = Some(5);
let y = filter::<OptionBrand, _, _>(|a| a > 2, x);
assert_eq!(y, Some(5));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§