pub trait Traversable: Functor + Foldable {
// Provided methods
fn traverse<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone, B: 'a + Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, Apply0L1T<F, B>>,
) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, Apply0L1T<F, Apply0L1T<Self, B>>>
where Apply0L1T<F, B>: Clone,
Apply0L1T<F, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, B>, Apply0L1T<Self, B>>>: Clone,
Apply0L1T<Self, B>: 'a,
Apply0L1T<Self, Apply0L1T<F, B>>: 'a { ... }
fn sequence<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone>(
t: Apply0L1T<Self, Apply0L1T<F, A>>,
) -> Apply0L1T<F, Apply0L1T<Self, A>>
where Apply0L1T<F, A>: 'a + Clone,
Apply0L1T<F, ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, Apply0L1T<Self, A>>>: Clone,
Apply0L1T<Self, A>: 'a,
Apply0L1T<Self, Apply0L1T<F, A>>: 'a,
Apply0L1T<F, Apply0L1T<Self, A>>: 'a { ... }
}
Expand description
A typeclass for traversable functors.
Traversable
functors can be traversed, which accumulates results and effects in some Applicative
context.
A minimum implementation of Traversable
requires the manual implementation of at least Traversable::traverse
or Traversable::sequence
.
Provided Methods§
Sourcefn traverse<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone, B: 'a + Clone>(
f: ApplyFn<'a, ClonableFnBrand, A, Apply0L1T<F, B>>,
) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, Apply0L1T<F, Apply0L1T<Self, B>>>
fn traverse<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone, B: 'a + Clone>( f: ApplyFn<'a, ClonableFnBrand, A, Apply0L1T<F, B>>, ) -> ApplyFn<'a, ClonableFnBrand, Apply0L1T<Self, A>, Apply0L1T<F, Apply0L1T<Self, B>>>
Map each element of the Traversable
structure to a computation, evaluate those computations and combine the results into an Applicative
context.
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 theTraversable
structure and outputs applicative computations for each.ta
: ATraversable
structure 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])
);
Sourcefn sequence<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone>(
t: Apply0L1T<Self, Apply0L1T<F, A>>,
) -> Apply0L1T<F, Apply0L1T<Self, A>>
fn sequence<'a, ClonableFnBrand: 'a + ClonableFn, F: Applicative, A: 'a + Clone>( t: Apply0L1T<Self, Apply0L1T<F, A>>, ) -> Apply0L1T<F, Apply0L1T<Self, A>>
Evaluate each computation in a Traversable
structure and accumulate the results into an Applicative
context.
The default implementation of sequence
is implemented in terms of traverse
and identity
where:
sequence = traverse identity
§Type Signature
forall t f a. Traversable t, Applicative f => t (f a) -> f (t a)
§Parameters
t
: ATraversable
structure containingApplicative
computations.
§Returns
An Applicative
containing the accumulated results of the computations.
§Examples
use fp_library::{brands::{VecBrand, OptionBrand, RcFnBrand}, functions::sequence};
assert_eq!(
sequence::<RcFnBrand, VecBrand, OptionBrand, i32>(vec![Some(1), Some(2), Some(3)]),
Some(vec![1, 2, 3])
);
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 OptionBrand
impl Traversable for SoloBrand
impl Traversable for VecBrand
impl<E> Traversable for ResultWithErrBrand<E>
§Examples
use fp_library::{brands::{ResultWithErrBrand, RcFnBrand, OptionBrand}, functions::traverse};
use std::rc::Rc;
assert_eq!(
traverse::<RcFnBrand, ResultWithErrBrand<String>, OptionBrand, i32, i32>(Rc::new(|x| Some(x * 2)))(Ok(3)),
Some(Ok(6))
);
assert_eq!(
traverse::<RcFnBrand, ResultWithErrBrand<String>, OptionBrand, i32, i32>(Rc::new(|x| Some(x * 2)))(Err("error".to_string())),
Some(Err("error".to_string()))
);