Reductor

Trait Reductor 

Source
pub trait Reductor<A>: Sized {
    type State;

    // Required methods
    fn new(item: A) -> Self::State;
    fn reduce(state: Self::State, item: A) -> Self::State;
    fn into_result(state: Self::State) -> Self;
}
Expand description

Reductors are types that implement the logic for folding an iterator into a single result.

use reductor::{Reductor, Reduce};

struct MeanState { mean: f32, count: usize };

struct Mean(f32);

impl Reductor<f32> for Mean {
    type State = MeanState;
     
    fn new(item: f32) -> Self::State {
        MeanState { mean: item, count: 1 }
    }

    fn reduce(acc: Self::State, item: f32) -> Self::State {
        MeanState {
            mean: acc.mean + item,
            count: acc.count + 1,
        }
    }
     
    fn into_result(state: Self::State) -> Self {
        Self(state.mean / state.count as f32)
    }
}

let Mean(mean) = vec![8.5, -5.5, 2.0, -4.0].into_iter()
    .reduce_with::<Option<Mean>>()
    .unwrap();

assert!((mean - 0.25).abs() < f32::EPSILON);

Required Associated Types§

Source

type State

Intermediate state for the reductor.

This type is used to keep track of the state of reduction while processing an iterator. The first item yielded is converted into the State type by calling new. The next item will be reduced using the previous state by calling reduce with the new item, and the resulting state will be used for the next reduction, and so forth. When the iterator is exhausted, the intermediate state will be turned into a result by calling into_result.

State must implement the Default trait for the Reductor to be used with reduce_with, otherwise, an initial state can be provided by calling fold_with.

Required Methods§

Source

fn new(item: A) -> Self::State

This method will be called with the first item yielded by an iterator to create the initial state of the reductor.

Source

fn reduce(state: Self::State, item: A) -> Self::State

Reduce the current accumulated state with the next item yielded by an iterator, returning the new state.

Source

fn into_result(state: Self::State) -> Self

After reducing the entire iterator, and exhausting it, turn the final state into a result.

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.

Implementations on Foreign Types§

Source§

impl<A1, A2, A3, A4, A5, R1, R2, R3, R4, R5> Reductor<(A1, A2, A3, A4, A5)> for (R1, R2, R3, R4, R5)
where R1: Reductor<A1>, R2: Reductor<A2>, R3: Reductor<A3>, R4: Reductor<A4>, R5: Reductor<A5>,

Source§

type State = (<R1 as Reductor<A1>>::State, <R2 as Reductor<A2>>::State, <R3 as Reductor<A3>>::State, <R4 as Reductor<A4>>::State, <R5 as Reductor<A5>>::State)

Source§

fn new(item: (A1, A2, A3, A4, A5)) -> Self::State

Source§

fn reduce(state: Self::State, item: (A1, A2, A3, A4, A5)) -> Self::State

Source§

fn into_result(state: Self::State) -> Self

Source§

impl<A1, A2, A3, A4, R1, R2, R3, R4> Reductor<(A1, A2, A3, A4)> for (R1, R2, R3, R4)
where R1: Reductor<A1>, R2: Reductor<A2>, R3: Reductor<A3>, R4: Reductor<A4>,

Source§

type State = (<R1 as Reductor<A1>>::State, <R2 as Reductor<A2>>::State, <R3 as Reductor<A3>>::State, <R4 as Reductor<A4>>::State)

Source§

fn new(item: (A1, A2, A3, A4)) -> Self::State

Source§

fn reduce(state: Self::State, item: (A1, A2, A3, A4)) -> Self::State

Source§

fn into_result(state: Self::State) -> Self

Source§

impl<A1, A2, A3, R1, R2, R3> Reductor<(A1, A2, A3)> for (R1, R2, R3)
where R1: Reductor<A1>, R2: Reductor<A2>, R3: Reductor<A3>,

Source§

type State = (<R1 as Reductor<A1>>::State, <R2 as Reductor<A2>>::State, <R3 as Reductor<A3>>::State)

Source§

fn new(item: (A1, A2, A3)) -> Self::State

Source§

fn reduce(state: Self::State, item: (A1, A2, A3)) -> Self::State

Source§

fn into_result(state: Self::State) -> Self

Source§

impl<A1, A2, R1, R2> Reductor<(A1, A2)> for (R1, R2)
where R1: Reductor<A1>, R2: Reductor<A2>,

Two Reductors can be combined in a tuple to reduce iterators that yield two-element tuples.

use reductor::{Reduce, Sum, Product};

let iter = (5..10).map(|x| (x, -(x as i64)));

let (Sum(sum), Product(product)) = iter
    .clone()
    .reduce_with::<(Sum<u64>, Product<i64>)>();

assert_eq!(sum, iter.clone().map(|(x, ..)| x).sum());
assert_eq!(product, iter.clone().map(|(.., x)| x).product())

See Reductors for reducing a single-item tuple with multiple Reductors.

Source§

type State = (<R1 as Reductor<A1>>::State, <R2 as Reductor<A2>>::State)

Source§

fn new(item: (A1, A2)) -> Self::State

Source§

fn reduce(state: Self::State, item: (A1, A2)) -> Self::State

Source§

fn into_result(state: Self::State) -> Self

Source§

impl<R, A> Reductor<A> for Option<R>
where R: Reductor<A>,

Wrapping a Reductor in an Option allows using reduce_with with a Reductor whose State does not implement Default.

let _ = (0..10).reduce_with::<Min<u32>>();
let _ = (0..10).reduce_with::<Option<Min<u32>>>();
Source§

type State = Option<<R as Reductor<A>>::State>

Source§

fn new(item: A) -> Self::State

Source§

fn reduce(state: Self::State, item: A) -> Self::State

Source§

fn into_result(state: Self::State) -> Self

Implementors§

Source§

impl Reductor<f32> for MaxF<Option<f32>>

Source§

type State = NonEmptyState<Option<f32>>

Source§

impl Reductor<f32> for MaxF<f32>

Source§

type State = NonEmptyState<f32>

Source§

impl Reductor<f32> for MinF<Option<f32>>

Source§

type State = NonEmptyState<Option<f32>>

Source§

impl Reductor<f32> for MinF<f32>

Source§

type State = NonEmptyState<f32>

Source§

impl Reductor<f32> for MinMaxF<Option<f32>>

Source§

impl Reductor<f32> for MinMaxF<f32>

Source§

impl Reductor<f64> for MaxF<Option<f64>>

Source§

type State = NonEmptyState<Option<f64>>

Source§

impl Reductor<f64> for MaxF<f64>

Source§

type State = NonEmptyState<f64>

Source§

impl Reductor<f64> for MinF<Option<f64>>

Source§

type State = NonEmptyState<Option<f64>>

Source§

impl Reductor<f64> for MinF<f64>

Source§

type State = NonEmptyState<f64>

Source§

impl Reductor<f64> for MinMaxF<Option<f64>>

Source§

impl Reductor<f64> for MinMaxF<f64>

Source§

impl<A> Reductor<A> for Count

Source§

impl<A> Reductor<A> for CountNonZero

Source§

impl<A> Reductor<A> for MinMax<Option<A>>
where A: Clone + Ord,

Source§

impl<A> Reductor<A> for MinMax<A>
where A: Clone + Ord,

Source§

type State = <Reductors<(Min<A>, Max<A>)> as Reductor<A>>::State

Source§

impl<A, R1, R2> Reductor<A> for Reductors<(R1, R2)>
where A: Clone, R1: Reductor<A>, R2: Reductor<A>,

Source§

type State = (<R1 as Reductor<A>>::State, <R2 as Reductor<A>>::State)

Source§

impl<A, R1, R2, R3> Reductor<A> for Reductors<(R1, R2, R3)>
where A: Clone, R1: Reductor<A>, R2: Reductor<A>, R3: Reductor<A>,

Source§

type State = (<R1 as Reductor<A>>::State, <R2 as Reductor<A>>::State, <R3 as Reductor<A>>::State)

Source§

impl<A, R1, R2, R3, R4> Reductor<A> for Reductors<(R1, R2, R3, R4)>
where A: Clone, R1: Reductor<A>, R2: Reductor<A>, R3: Reductor<A>, R4: Reductor<A>,

Source§

type State = (<R1 as Reductor<A>>::State, <R2 as Reductor<A>>::State, <R3 as Reductor<A>>::State, <R4 as Reductor<A>>::State)

Source§

impl<A, R1, R2, R3, R4, R5> Reductor<A> for Reductors<(R1, R2, R3, R4, R5)>
where A: Clone, R1: Reductor<A>, R2: Reductor<A>, R3: Reductor<A>, R4: Reductor<A>, R5: Reductor<A>,

Source§

type State = (<R1 as Reductor<A>>::State, <R2 as Reductor<A>>::State, <R3 as Reductor<A>>::State, <R4 as Reductor<A>>::State, <R5 as Reductor<A>>::State)

Source§

impl<A, T> Reductor<A> for Product<T>
where T: Product + Product<A>,

Source§

type State = State<T>

Source§

impl<A, T> Reductor<A> for Sum<T>
where T: Sum + Sum<A>,

Source§

type State = T

Source§

impl<T> Reductor<T> for Max<Option<T>>
where T: Ord,

Source§

type State = NonEmptyState<Option<T>>

Source§

impl<T> Reductor<T> for Max<T>
where T: Ord,

Source§

type State = NonEmptyState<T>

Source§

impl<T> Reductor<T> for Mean<f32>
where T: Into<f32>,

Source§

type State = NonEmptyState<(f32, usize)>

Source§

impl<T> Reductor<T> for Mean<f64>
where T: Into<f64>,

Source§

type State = NonEmptyState<(f64, usize)>

Source§

impl<T> Reductor<T> for Min<Option<T>>
where T: Ord,

Source§

type State = NonEmptyState<Option<T>>

Source§

impl<T> Reductor<T> for Min<T>
where T: Ord,

Source§

type State = NonEmptyState<T>