dnsl48 / fraction

[Rust] Lossless fractions and decimals; drop-in float replacement
Apache License 2.0
83 stars 25 forks source link

Allow usage with summing iterators #22

Closed eutampieri closed 4 years ago

eutampieri commented 4 years ago

It would be useful if you implemented the Sum trait in order to use the .sum() on an iterator over a vec of fractions. At the moment it fails with the following error:

the trait bound `fraction::fraction::GenericFraction<u64>: std::iter::Sum` is not satisfied
the trait `std::iter::Sum` is not implemented for `fraction::fraction::GenericFraction<u64>`rustc(E0277)
eutampieri commented 4 years ago

I've just added this to fraction/mod.rs and it works

impl<T: Clone + Integer> Sum for GenericFraction<T> {
    fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
        iter.fold(GenericFraction::<T>::neg_zero(), Add::add)
    }
}
dnsl48 commented 4 years ago

Hi @eutampieri, thank you for the idea. Fair enough.

I think that would also make sense to add Product implementation. Would you like to contribute a PR? Ideally that would be an implementation of both traits for Fraction and Decimal and some test coverage for them. If you don't have time, I'll prepare a patch for the next minor release myself, no pressure :)