Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-consuming operations #3

Open
kyleheadley opened this issue Aug 16, 2016 · 3 comments
Open

Support non-consuming operations #3

kyleheadley opened this issue Aug 16, 2016 · 3 comments

Comments

@kyleheadley
Copy link

I'm calculating both mean and stddev on some data, but I find that I have to clone my data vec to do it. The functions as they are consume an iterator, whose item type must implement ToPrimitive. But ToPrimitive is not defined over &i32 and the like, so I can't use, for example, mean(vec![1,2,3].iter()). I had to use something like mean(vec![1,2,3].clone().into_iter(). I had small examples, but I think you want this library to scale, and cloning data to calculate stats won't scale.

@BurntSushi
Copy link
Owner

This should work:

let xs = vec![1, 2, 3];
let avg = mean(xs.iter().cloned());
println!("xs is still mine: {:?}", xs);

The trick here is that xs.iter() provides an Iterator<Item=&i32> and cloned turns the &i32 into an i32, which should be optimized out.

@kyleheadley
Copy link
Author

Ok, that optimization should work in the common cases. However, it's still cloning before converting to an f64, rather than converting from a reference to the data. You'll have to suggest to people to impl ToPrimitive for &'a Bigdata if for some reason they have a Bigdata type that converts to a primitive and want stats.

@BurntSushi
Copy link
Owner

Yes, if your type doesn't impl Copy then your suggestion sounds good. I think all of the types that impl ToPrimitive in the num_traits crate impl Copy except for BigInt and BigUint.

More broadly: I don't really have a good vision for this crate. My hope is that something more cohesive takes its place, but I'm not really the right person to lead that charge. I should probably put that in the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants