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§
Sourcetype State
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§
Sourcefn new(item: A) -> Self::State
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.
Sourcefn reduce(state: Self::State, item: A) -> Self::State
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.
Sourcefn into_result(state: Self::State) -> Self
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)
impl<A1, A2, A3, A4, A5, R1, R2, R3, R4, R5> Reductor<(A1, A2, A3, A4, A5)> for (R1, R2, R3, R4, R5)
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)
fn new(item: (A1, A2, A3, A4, A5)) -> Self::State
fn reduce(state: Self::State, item: (A1, A2, A3, A4, A5)) -> Self::State
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)
impl<A1, A2, A3, A4, R1, R2, R3, R4> Reductor<(A1, A2, A3, A4)> for (R1, R2, R3, R4)
type State = (<R1 as Reductor<A1>>::State, <R2 as Reductor<A2>>::State, <R3 as Reductor<A3>>::State, <R4 as Reductor<A4>>::State)
fn new(item: (A1, A2, A3, A4)) -> Self::State
fn reduce(state: Self::State, item: (A1, A2, A3, A4)) -> Self::State
fn into_result(state: Self::State) -> Self
Source§impl<A1, A2, A3, R1, R2, R3> Reductor<(A1, A2, A3)> for (R1, R2, R3)
impl<A1, A2, A3, R1, R2, R3> Reductor<(A1, A2, A3)> for (R1, R2, R3)
Source§impl<A1, A2, R1, R2> Reductor<(A1, A2)> for (R1, R2)
Two Reductors can be combined in a tuple to reduce iterators that yield two-element tuples.
impl<A1, A2, R1, R2> Reductor<(A1, A2)> for (R1, R2)
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§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.
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>>>();