traverse

Function traverse 

Source
pub fn traverse<'a, Brand: Traversable<'a>, F: Applicative, A: 'a + Clone, B: 'a + Clone>(
    f: ArcFn<'a, A, Apply1<F, B>>,
) -> ArcFn<'a, Apply1<Brand, A>, Apply1<F, Apply1<Brand, B>>>
where Apply1<F, B>: 'a + Clone, Apply1<F, ArcFn<'a, Apply1<Brand, B>, Apply1<Brand, B>>>: Clone,
Expand description

Map each element of the Traversable structure to a computation, evaluate those computations and combine the results into an Applicative context.

Free function version that dispatches to the typeclass’ associated function.

The default implementation of traverse is implemented in terms of sequence and map where:

(traverse f) ta = sequence ((map f) ta)

§Type Signature

forall t f a b. Traversable t, Applicative f => (a -> f b) -> t a -> f (t b)

§Parameters

  • f: A function that inputs the elements in the Traversable structure and outputs applicative computations for each.
  • ta: A Traversable structure containing values.

§Returns

An Applicative containing the accumulated results of the computations.

§Examples

use fp_library::{brands::{VecBrand, OptionBrand}, functions::traverse};
use std::sync::Arc;

assert_eq!(
    traverse::<VecBrand, OptionBrand, i32, i32>(Arc::new(|x| Some(x * 2)))(vec![1, 2, 3]),
    Some(vec![2, 4, 6])
);