I have a specific use case where I need to choose the maximum element from an Iterator<Item = (i8, f64)>
. If there are multiple maximum elements in the Iterator I want to randomly select a maximum element uniformly distributed.
I tried the max_by
function for Iterators but it doesn't work as I need it because it always returns the last element if there a multiple maximum elements.
Here the example:
fn main() {
let v = [(-1i8, 0.4f64), (0, 0.2), (1, 0.4)];
let max = v.into_iter().max_by(|(_, r), (_, s)| r.total_cmp(s)).unwrap(); //always returns (1, 0.4)
println!("{:?}", max);
}
I need a function which returns another Iterator over the maximum elements. Then I could choose a random element from that Iterator.
Not in std, but in itertools
there is:
iter.max_set();