Trait rs3cache_utils::adapters::Accumulator
source · pub trait Accumulator: Iterator {
// Provided method
fn accumulate<F>(self, f: F) -> Accumulate<Self, F> ⓘ
where F: Fn(Self::Item, Self::Item) -> Self::Item,
Self::Item: Copy,
Self: Sized + Iterator { ... }
}
Expand description
An iterator to maintain state while iterating another iterator.
Accumulate
is created by the accumulate
method on Iterator
. See its
documentation for more.
Provided Methods§
sourcefn accumulate<F>(self, f: F) -> Accumulate<Self, F> ⓘwhere
F: Fn(Self::Item, Self::Item) -> Self::Item,
Self::Item: Copy,
Self: Sized + Iterator,
fn accumulate<F>(self, f: F) -> Accumulate<Self, F> ⓘwhere F: Fn(Self::Item, Self::Item) -> Self::Item, Self::Item: Copy, Self: Sized + Iterator,
An iterator adaptor that yields the computation of the closure F
over the
accumulated value and the next element of the preceding iterator.
accumulate()
takes a function or closure with two arguments,
the first being the previous yielded value and the second an element from the preceding iterator.
The iterator maintains the state of the last yielded value.
The first element of the preceding iterator is yielded as-is.
Examples
Basic usage:
use rs3cache::utils::adapters::Accumulator;
let mut iter = (1..6).accumulate(|x, y| x + y);
assert_eq!(iter.next(), Some(1)); // 1
assert_eq!(iter.next(), Some(3)); // 1+2
assert_eq!(iter.next(), Some(6)); // (1+2) + 3
assert_eq!(iter.next(), Some(10)); // (1+2+3) + 4
assert_eq!(iter.next(), Some(15)); // (1+2+3+4) + 5
assert_eq!(iter.next(), None);