Traversable

Trait Traversable 

Source
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§

Source

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,

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 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, 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])
);
Source

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,

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
§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§

Source§

impl Traversable for OptionBrand

Source§

impl Traversable for SoloBrand

Source§

impl Traversable for VecBrand

Source§

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()))
);
Source§

impl<First> Traversable for PairWithFirstBrand<First>
where First: Clone,

Source§

impl<Second> Traversable for PairWithSecondBrand<Second>
where Second: Clone,

Source§

impl<T> Traversable for ResultWithOkBrand<T>