pub trait Traversable: Functor + Foldable {
// Provided methods
fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>
where Func: Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
<F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone { ... }
fn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>
where <F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone { ... }
}Expand description
A type class for traversable functors.
Traversable functors can be traversed, which accumulates results and effects in some Applicative context.
Provided Methods§
Sourcefn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
<F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
func: Func,
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>>where
Func: Fn(A) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, B> + 'a,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
<F as Kind_cdc7cd43dac7585f>::Of<'a, B>: Clone,
Map each element of the Traversable structure to a computation, evaluate those computations and combine the results into an Applicative context.
The default implementation is defined in terms of sequence and map.
Note: This default implementation may be less efficient than a direct implementation because it performs two passes: first mapping the function to create an intermediate structure of computations, and then sequencing that structure. A direct implementation can often perform the traversal in a single pass without allocating an intermediate container. Types should provide their own implementation if possible.
§Type Signature
forall A B F. Applicative F => (A -> F B, Self A) -> F (Self B)
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the traversable structure.B: The type of the elements in the resulting traversable structure.F: The applicative context.Func: The type of the function to apply.
§Parameters
func: The function to apply to each element, returning a value in an applicative context.ta: The traversable structure.
§Returns
The traversable structure wrapped in the applicative context.
§Examples
use fp_library::{functions::*, brands::*};
let x = Some(5);
let y = traverse::<OptionBrand, _, _, OptionBrand, _>(|a| Some(a * 2), x);
assert_eq!(y, Some(Some(10)));Sourcefn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>where
<F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
fn sequence<'a, A: 'a + Clone, F: Applicative>(
ta: <Self as Kind_cdc7cd43dac7585f>::Of<'a, <F as Kind_cdc7cd43dac7585f>::Of<'a, A>>,
) -> <F as Kind_cdc7cd43dac7585f>::Of<'a, <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>>where
<F as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
<Self as Kind_cdc7cd43dac7585f>::Of<'a, A>: Clone,
Evaluate each computation in a Traversable structure and accumulate the results into an Applicative context.
The default implementation is defined in terms of traverse and identity.
§Type Signature
forall A F. Applicative F => Self (F A) -> F (Self A)
§Type Parameters
'a: The lifetime of the elements.A: The type of the elements in the traversable structure.F: The applicative context.
§Parameters
ta: The traversable structure containing values in an applicative context.
§Returns
The traversable structure wrapped in the applicative context.
§Examples
use fp_library::{functions::*, brands::*};
let x = Some(Some(5));
let y = sequence::<OptionBrand, _, OptionBrand>(x);
assert_eq!(y, Some(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§
impl Traversable for CatListBrand
impl Traversable for IdentityBrand
impl Traversable for OptionBrand
impl Traversable for Tuple1Brand
impl Traversable for VecBrand
impl<DoneType: Clone + 'static> Traversable for StepWithDoneBrand<DoneType>
§Type Parameters
DoneType: The done type.
impl<E: Clone + 'static> Traversable for ResultWithErrBrand<E>
§Type Parameters
E: The error type.
impl<First: Clone + 'static> Traversable for PairWithFirstBrand<First>
§Type Parameters
First: The type of the first value in the pair.
§Type Parameters
First: The type of the first value in the pair.
impl<First: Clone + 'static> Traversable for Tuple2WithFirstBrand<First>
§Type Parameters
First: The type of the first value in the tuple.
impl<LoopType: Clone + 'static> Traversable for StepWithLoopBrand<LoopType>
§Type Parameters
LoopType: The loop type.
impl<Second: Clone + 'static> Traversable for PairWithSecondBrand<Second>
§Type Parameters
Second: The type of the second value in the pair.
§Type Parameters
Second: The type of the second value in the pair.
impl<Second: Clone + 'static> Traversable for Tuple2WithSecondBrand<Second>
§Type Parameters
Second: The type of the second value in the tuple.
impl<T: Clone + 'static> Traversable for ResultWithOkBrand<T>
§Type Parameters
T: The success type.