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

Allow usage with summing iterators #22

Closed
eutampieri opened this issue May 4, 2020 · 2 comments · Fixed by #23
Closed

Allow usage with summing iterators #22

eutampieri opened this issue May 4, 2020 · 2 comments · Fixed by #23

Comments

@eutampieri
Copy link
Contributor

eutampieri commented May 4, 2020

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
Copy link
Contributor Author

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
Copy link
Owner

dnsl48 commented May 4, 2020

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 :)

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

Successfully merging a pull request may close this issue.

2 participants