average icon indicating copy to clipboard operation
average copied to clipboard

impl Extend<f64> for Variance

Open Pardoxa opened this issue 3 years ago • 2 comments

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

Pardoxa avatar Feb 09 '22 11:02 Pardoxa

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.

vks avatar Feb 09 '22 11:02 vks

That certainly makes more sense than collecting that into a Vector first.

Thank you 🙂

Pardoxa avatar Feb 09 '22 12:02 Pardoxa

Implemented in #21.

vks avatar Jul 04 '23 17:07 vks