On Tue, Sep 24, 2013 at 9:50 PM, Kevin Ballard <[email protected]> wrote:
> Maybe we should just implement AdditiveIterator on &A as well? That way > you can just say `values.iter().sum()`. > > -Kevin > > On Sep 24, 2013, at 6:19 PM, Daniel Micay <[email protected]> wrote: > > On Tue, Sep 24, 2013 at 8:37 PM, Tim Kuehn <[email protected]> wrote: > >> There's also std::iter::AdditiveIterator. >> >> fn average<T: Num + NumCast + Clone>(values: &[T]) -> T { >> let sum = values.iter().map(|n| n.clone()).sum(); >> sum / num::cast(values.len()) >> } >> > > Using `iter().fold(Zero::zero(), |a, b| a + *b)` would be a lot more > efficient for big integers. I'm not sure if we should really have the > `sum`, `product`, `max` and `min` methods. > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > > > It won't easily work, because it expects `Add<T, T>` meaning `+` has to return the same type as it receives.
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
