Changing all values in vector

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 :frowning:

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.});
}
1 Like
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.

2 Likes

Thanks, I understood the .count() had been added just to consume the iter() as without consuming it nothing will happen.

Can you pls explain me the f64::max(acc, *x) how it works, I did not get it even in the sample code I provided.

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.

1 Like

Thanks, What I meant is:
How the comparison is done using this simple line to find the max, I did not get it.

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.

2 Likes

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?

f64 only implements PartialOrd, Iterator::max requires Ord.

2 Likes

Note that this is because of NaN. It doesn't know where to put them, so it doesn't let you sort floats.

1 Like