impl Extend<f64> for Variance
I currently have an Iterator, that yields two measurable quantities of a system. If one quantity is already measured, measuring the other is way less effort, so it makes sense to return both. I want to calculate the variance of them.
Currently I have to to something like:
let (a, b): (Vec<_>, Vec<_>) = (0..10)
.map(|v| (v as f64, (v*2) as f64)) // do complicated stuff
.unzip();
let var_a: Variance = a.iter().collect();
let var_b: Variance = b.iter().collect();
But now I have to construct two Vectors that I do not really need. I would prefer to do something like this:
let (var_a, var_b): (Variance, Variance) = (0..10)
.map(|v| (v as f64, (v*2) as f64))
.unzip();
For that to work, the trait Extend<f64> needs to be implemented.
I therefore would appreciate the implementation of that trait very much :) Thanks for your work!
PS: I hope this is the right place to ask, as I am not that familiar with git issues
For now, you can just use a for loop:
let mut var_a = Variance::new();
let mut var_b = Variance::new();
for v in 0..10 {
var_a.add(v);
var_b.add(v*2);
}
Implementing Extend certainly makes sense though, I'll look into this.
That certainly makes more sense than collecting that into a Vector first.
Thank you 🙂
Implemented in #21.