pub trait Compactable: Kind_cdc7cd43dac7585f {
// Required methods
fn compact<'a, A: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>;
fn separate<'a, E: 'a, O: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>,
) -> (<Self as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, O>);
}Expand description
A type class for data structures that can be compacted and separated.
Compactable allows for:
compact: Filtering outNonevalues and unwrappingSomevalues from a structure ofOptions.separate: Splitting a structure ofResults into a pair of structures, one containing theErrvalues and the other containing theOkvalues.
§Laws
To be Compactable alone, no laws must be satisfied other than the type signature.
If the data type is also a Functor:
- Identity:
compact(map(Some, fa)) = fa.
If the data type is also Plus:
- Annihilation (empty):
compact(empty) = empty. - Annihilation (map):
compact(map(|_| None, xs)) = empty.
§Examples
Compactable laws for Option:
use fp_library::{
brands::*,
functions::*,
};
// Functor Identity: compact(map(Some, fa)) = fa
assert_eq!(compact::<OptionBrand, _>(map::<OptionBrand, _, _>(Some, Some(5))), Some(5),);
assert_eq!(compact::<OptionBrand, _>(map::<OptionBrand, _, _>(Some, None::<i32>)), None,);
// Plus Annihilation (empty): compact(empty) = empty
assert_eq!(
compact::<OptionBrand, _>(plus_empty::<OptionBrand, Option<i32>>()),
plus_empty::<OptionBrand, i32>(),
);
// Plus Annihilation (map): compact(map(|_| None, xs)) = empty
assert_eq!(
compact::<OptionBrand, _>(map::<OptionBrand, _, _>(|_: i32| None::<i32>, Some(5))),
plus_empty::<OptionBrand, i32>(),
);Compactable laws for Vec:
use fp_library::{
brands::*,
functions::*,
};
// Functor Identity: compact(map(Some, fa)) = fa
assert_eq!(compact::<VecBrand, _>(map::<VecBrand, _, _>(Some, vec![1, 2, 3])), vec![1, 2, 3],);
// Plus Annihilation (empty): compact(empty) = empty
assert_eq!(
compact::<VecBrand, _>(plus_empty::<VecBrand, Option<i32>>()),
plus_empty::<VecBrand, i32>(),
);
// Plus Annihilation (map): compact(map(|_| None, xs)) = empty
assert_eq!(
compact::<VecBrand, _>(map::<VecBrand, _, _>(|_: i32| None::<i32>, vec![1, 2, 3])),
plus_empty::<VecBrand, i32>(),
);Required Methods§
Sourcefn compact<'a, A: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
fn compact<'a, A: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <OptionBrand as Kind_cdc7cd43dac7585f>::Of<'a, A>>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>
Compacts a data structure of Options, discarding None values and keeping Some values.
§Type Signature
forall A. Self (Option A) -> Self A
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in theOption.
§Parameters
fa: The data structure containingOptionvalues.
§Returns
A new data structure containing only the values from the Some variants.
§Examples
use fp_library::{
brands::*,
functions::*,
};
let x = Some(Some(5));
let y = compact::<OptionBrand, _>(x);
assert_eq!(y, Some(5));
let z = Some(None::<i32>);
let w = compact::<OptionBrand, _>(z);
assert_eq!(w, None);Sourcefn separate<'a, E: 'a, O: 'a>(
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>,
) -> (<Self as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, O>)
fn separate<'a, E: 'a, O: 'a>( fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, Result<O, E>>, ) -> (<Self as Kind_cdc7cd43dac7585f>::Of<'a, E>, <Self as Kind_cdc7cd43dac7585f>::Of<'a, O>)
Separates a data structure of Results into two data structures: one containing the Err values and one containing the Ok values.
§Type Signature
forall E O. Self (Result O E) -> (Self E, Self O)
§Type Parameters
'a: The lifetime of the elements.E: The type of the error values.O: The type of the success values.
§Parameters
fa: The data structure containingResultvalues.
§Returns
A pair of data structures: the first containing the Err values, and the second containing the Ok values.
§Examples
use fp_library::{
brands::*,
functions::*,
};
let x: Option<Result<i32, &str>> = Some(Ok(5));
let (errs, oks) = separate::<OptionBrand, _, _>(x);
assert_eq!(oks, Some(5));
assert_eq!(errs, None);
let y: Option<Result<i32, &str>> = Some(Err("error"));
let (errs2, oks2) = separate::<OptionBrand, _, _>(y);
assert_eq!(oks2, None);
assert_eq!(errs2, Some("error"));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.