pub enum MinMax<Item> {
None,
Single(Item),
Both(Item, Item),
}Expand description
The MinMax type uses the std::cmp::PartialOrd trait to
contain both the smallest and largest iterated values.
Example:
use autofolder::*;
// Create an autofolder that collects the min+max usize.
let mut minmax = MinMax::<usize>::default();
// We can "reduce-in" individual items.
// (note: as this is the first value, we incorporate it
// without calling the trait function)
minmax.reduce(3);
// Minmax fills up `min` first, so at this moment we can see that
// min is all we have:
println!("Partial minmax is {:?}", minmax.as_ref());
// `eval` does the same as `reduce`, and in this case sets
// min=2 and max=3:
minmax.eval(2);
// We can now peek at the running output as a single `Option`:
println!("Partial minmax is {:?}", minmax.as_ref());
// And still keep on folding by processing whole iterators:
minmax.extend((1..=5));
// And finally consume the autofolder to get the final output value:
let (min, max) = minmax.to_inner().unwrap();
println!("Final min is {}, max is {}", min, max);Variants§
None
Empty; no item evaluated.
Single(Item)
Single item evaluated.
Both(Item, Item)
Two or more items evaluated - min and max values.
Implementations§
Source§impl<Item> MinMax<Item>
impl<Item> MinMax<Item>
Sourcepub fn to_inner(self) -> Option<(Item, Item)>where
Item: Clone,
pub fn to_inner(self) -> Option<(Item, Item)>where
Item: Clone,
Deconstruct self and return the inner values that were found.
Sourcepub fn as_ref(&self) -> Option<(&Item, &Item)>
pub fn as_ref(&self) -> Option<(&Item, &Item)>
Returns a reference to the inner values, if they exist.
Sourcepub fn min_as_ref(&self) -> Option<&Item>
pub fn min_as_ref(&self) -> Option<&Item>
Returns a reference to the min inner values, if it exist.
Sourcepub fn max_as_ref(&self) -> Option<&Item>
pub fn max_as_ref(&self) -> Option<&Item>
Returns a reference to the max inner values, if it exist.
Sourcepub fn reduce(&mut self, item: Item)where
Item: PartialOrd,
pub fn reduce(&mut self, item: Item)where
Item: PartialOrd,
Replaces a current value with the new one if the new one is greater/smaller.
When we have a single value, min is always filled up first,
and then swapped with max if necessary.
Sourcepub fn reduce_ref(&mut self, item: &Item)where
Item: PartialOrd + Clone,
pub fn reduce_ref(&mut self, item: &Item)where
Item: PartialOrd + Clone,
Replaces a current value with the one behind the ref if it is greater/smaller.
When we have a single value, min is always filled up first,
and then swapped with max if necessary.
This function requires the Clone trait, but uses it only if necessary.
Sourcepub fn eval(&mut self, item: Item)where
Item: PartialOrd,
pub fn eval(&mut self, item: Item)where
Item: PartialOrd,
Alias for MinMax::reduce
Sourcepub fn eval_ref(&mut self, item: &Item)where
Item: PartialOrd + Clone,
pub fn eval_ref(&mut self, item: &Item)where
Item: PartialOrd + Clone,
Alias for MinMax::reduce_ref
Trait Implementations§
Source§impl<'a, Item> Extend<&'a Item> for MinMax<Item>where
Item: PartialOrd + Clone,
impl<'a, Item> Extend<&'a Item> for MinMax<Item>where
Item: PartialOrd + Clone,
Source§fn extend<It: IntoIterator<Item = &'a Item>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = &'a Item>>(&mut self, iter: It)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<Item> Extend<Item> for MinMax<Item>where
Item: PartialOrd,
impl<Item> Extend<Item> for MinMax<Item>where
Item: PartialOrd,
Source§fn extend<It: IntoIterator<Item = Item>>(&mut self, iter: It)
fn extend<It: IntoIterator<Item = Item>>(&mut self, iter: It)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)