pub fn traverse<'a, ClonableFnBrand: 'a + ClonableFn, Brand: Traversable, F: Applicative, A: Clone, B: 'a + Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, Apply0L1T<F, B>>,
) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Brand, A>, Apply0L1T<F, Apply0L1T<Brand, B>>>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 type class’ 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 a b. Traversable t, Applicative f => (a -> f b) -> t a -> f (t b)
§Parameters
f: A function that inputs the elements in theTraversablestructure and outputs applicative computations for each.ta: ATraversablestructure containing values.
§Returns
An Applicative containing the accumulated results of the computations.
§Examples
use fp_library::{brands::{VecBrand, OptionBrand, RcFnBrand}, functions::traverse};
use std::rc::Rc;
assert_eq!(
traverse::<RcFnBrand, VecBrand, OptionBrand, i32, i32>(Rc::new(|x| Some(x * 2)))(vec![1, 2, 3]),
Some(vec![2, 4, 6])
);