Traversable

Trait Traversable 

Source
pub trait Traversable<'a>: Functor + Foldable {
    // Provided methods
    fn traverse<F: Applicative, A: 'a + Clone, B: 'a + Clone>(
        f: ArcFn<'a, A, Apply1<F, B>>,
    ) -> ArcFn<'a, Apply1<Self, A>, Apply1<F, Apply1<Self, B>>>
       where Apply1<F, B>: 'a + Clone,
             Apply1<F, ArcFn<'a, Apply1<Self, B>, Apply1<Self, B>>>: Clone { ... }
    fn sequence<F: Applicative, A: 'a + Clone>(
        t: Apply1<Self, Apply1<F, A>>,
    ) -> Apply1<F, Apply1<Self, A>>
       where Apply1<F, A>: 'a + Clone,
             Apply1<F, ArcFn<'a, Apply1<Self, A>, Apply1<Self, A>>>: Clone { ... }
}
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<F: Applicative, A: 'a + Clone, B: 'a + Clone>( f: ArcFn<'a, A, Apply1<F, B>>, ) -> ArcFn<'a, Apply1<Self, A>, Apply1<F, Apply1<Self, B>>>
where Apply1<F, B>: 'a + Clone, Apply1<F, ArcFn<'a, Apply1<Self, B>, Apply1<Self, 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 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])
);
Source

fn sequence<F: Applicative, A: 'a + Clone>( t: Apply1<Self, Apply1<F, A>>, ) -> Apply1<F, Apply1<Self, A>>
where Apply1<F, A>: 'a + Clone, Apply1<F, ArcFn<'a, Apply1<Self, A>, Apply1<Self, A>>>: Clone,

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}, functions::sequence};
use std::sync::Arc;

assert_eq!(
    sequence::<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<'a> Traversable<'a> for OptionBrand

Source§

impl<'a> Traversable<'a> for SoloBrand

§Examples

use fp_library::{brands::{SoloBrand, OptionBrand}, functions::traverse, types::Solo};
use std::sync::Arc;

assert_eq!(
    traverse::<SoloBrand, OptionBrand, i32, i32>(Arc::new(|x| Some(x * 2)))(Solo(3)),
    Some(Solo(6))
);
assert_eq!(
    traverse::<SoloBrand, OptionBrand, i32, i32>(Arc::new(|x| None))(Solo(3)),
    None
);
Source§

impl<'a> Traversable<'a> for VecBrand

Source§

impl<'a, E> Traversable<'a> for ResultWithErrBrand<E>

§Examples

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

assert_eq!(
    traverse::<ResultWithErrBrand<String>, OptionBrand, i32, i32>(Arc::new(|x| Some(x * 2)))(Ok(3)),
    Some(Ok(6))
);
assert_eq!(
    traverse::<ResultWithErrBrand<String>, OptionBrand, i32, i32>(Arc::new(|x| Some(x * 2)))(Err("error".to_string())),
    Some(Err("error".to_string()))
);
Source§

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

Source§

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

Source§

impl<'a, T> Traversable<'a> for ResultWithOkBrand<T>