Filterable

Trait Filterable 

Source
pub trait Filterable: Compactable + Functor {
    // Provided methods
    fn partition_map<'a, Func, A: 'a, E: 'a, O: 'a>(
        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, Func, A: 'a + Clone>(
        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, Func, A: 'a, B: 'a>(
        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, Func, A: 'a + Clone>(
        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.

Provided Methods§

Source

fn partition_map<'a, Func, A: 'a, E: 'a, O: 'a>( 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 Functor::map and Compactable::separate.

§Type Signature

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

§Type Parameters
  • Func: The type of the function to apply.
  • A: The type of the elements in the input structure.
  • E: The type of the error values.
  • O: The type of the success values.
§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::classes::filterable::Filterable;
use fp_library::brands::OptionBrand;
use fp_library::types::Pair;

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

fn partition<'a, Func, A: 'a + Clone>( 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 a f. Filterable f => (a -> bool) -> f a -> (f a, f a)

§Type Parameters
  • Func: The type of the predicate function.
  • A: The type of the elements in the structure.
§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::classes::filterable::Filterable;
use fp_library::brands::OptionBrand;
use fp_library::types::Pair;

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

fn filter_map<'a, Func, A: 'a, B: 'a>( 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 Functor::map and Compactable::compact.

§Type Signature

forall a b f. Filterable f => (a -> Option b) -> f a -> f b

§Type Parameters
  • Func: The type of the function to apply.
  • A: The type of the elements in the input structure.
  • B: The type of the elements in the output structure.
§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::classes::filterable::Filterable;
use fp_library::brands::OptionBrand;

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

fn filter<'a, Func, A: 'a + Clone>( 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 a f. Filterable f => (a -> bool) -> f a -> f a

§Type Parameters
  • Func: The type of the predicate function.
  • A: The type of the elements in the structure.
§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::classes::filterable::Filterable;
use fp_library::brands::OptionBrand;

let x = Some(5);
let y = OptionBrand::filter(|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§