I need to change all values in vector x, if the value equal to the max make it 1 otherwise make it 0, looking for the simplest way to do so using functional programming, tried the below but it failed
fn main() {
let mut x = vec![1.,3.,2.];
let mut max = x.iter().cloned().fold(0./0. /* neg inf */, f64::max); // f64::max => f64::max(acc, v)
// Change all values in vector x, if the value equal to the max make it 1 otherwise make it 0
x.iter_mut().map(|v| if v < &mut max {0.} else {1.});
}
fn main() {
let mut x = vec![1.,3.,2.];
let max = x.iter().fold(std::f64::NEG_INFINITY, |acc, x| f64::max(acc, *x));
x.iter_mut().map(|v| *v = if *v < max {0.} else {1.}).count();
}
Being non-ownership-taking, iter() and iter_mut() will give out references.
As mentioned above, iter() gives out (immutable) references, so the parameter x of the closure supplied to fold() is of the type &f64. Since f64::max() wants owned values, x must be dereferenced, hence *x.
Ah, sorry, I see what you mean. Try to work it out by hand: acc starts at โโ and, at the end of each iteration, becomes the maximum of its previous value and the current slice element. At the end, it will contain the largest element of the slice.
Doesn't the standard library have a max call on an iterator, to simplify that max expression? Thus instead of let max = x.iter().fold(std::f64::NEG_INFINITY, |acc, x| f64::max(acc, *x)); it could be let max = x.iter().max(); or so? Or does it not work on reference values?