Skip to main content

Witherable

Trait Witherable 

Source
pub trait Witherable: Filterable + Traversable {
    // Provided methods
    fn wilt<'a, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>(
        func: Func,
        ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>>
       where Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>> + 'a,
             <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,
             <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone { ... }
    fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>(
        func: Func,
        ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
    ) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>
       where Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>> + 'a,
             <Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,
             <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone { ... }
}
Expand description

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

Witherable extends Filterable and Traversable, adding methods for:

  • wither: Effectful filter_map.
  • wilt: Effectful partition_map.

§Minimal Implementation

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

However, it is recommended to implement Witherable::wilt and Witherable::wither to avoid the intermediate structure created by the default implementations (which use traverse followed by separate or compact).

Provided Methods§

Source

fn wilt<'a, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>( func: Func, ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Pair<<Self as Kind_cdc7cd43dac7585f>::Of<'a, O>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, E>>>
where Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>> + 'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone, <M as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>: Clone,

Partitions a data structure based on a function that returns a Result in an applicative context.

The default implementation uses traverse and separate.

§Type Signature

forall self m a o e. (Witherable self, Applicative m) => (a -> m (Result o e), self a) -> m (Pair (self o) (self e))

§Type Parameters
  • 'a: The lifetime of the elements.
  • M: The applicative context.
  • 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 in an applicative context.
  • ta: The data structure to partition.
§Returns

The partitioned data structure wrapped in the applicative context.

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

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

fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>( func: Func, ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>
where Func: Fn(A) -> <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>> + 'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone, <M as Kind_cdc7cd43dac7585f>::Of<'a, Option<B>>: Clone,

Maps a function over a data structure and filters out None results in an applicative context.

The default implementation uses traverse and compact.

§Type Signature

forall self m a b. (Witherable self, Applicative m) => (a -> m (Option b), self a) -> m (self b)

§Type Parameters
  • 'a: The lifetime of the elements.
  • M: The applicative context.
  • 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 in an applicative context.
  • ta: The data structure to filter and map.
§Returns

The filtered and mapped data structure wrapped in the applicative context.

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

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

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§