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

Add CheckedSum and CheckedProduct traits #251

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wainwrightmark
Copy link

Closes #250

This adds CheckedSum and CheckedProduct traits as well as blanket implementations for them.

It also adds CheckedSumIter and CheckedProductIter traits and blanket implementations for those as that provides a much more ergonomic interface, allowing you to do:

asserteq!(Some(42), [40,2].iter().checked_sum());
asserteq!(Some(42), [40,2].into_iter().checked_sum());

Copy link
Member

@cuviper cuviper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a new iter module here, I think it would make more sense to move this to the num-iter crate.

/// Multiplies up an empty iterator returns a value representing One.
///
/// If the iterator contains Zero, the order of elements may effect whether the result is `None`.
fn checked_product<I: Iterator<Item = Self>>(iter: I) -> Option<Result>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is opposite of how Product is defined, where the type parameter is the input (Item) and it always outputs Self. I would rather match that for consistency, and same for Sum.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I completely agree. I feel very silly for having done it the other way around. I have changed it now.

/// Multiplies up an empty iterator returns a value representing One.
///
/// If the iterator contains Zero, the order of elements may effect whether the result is `None`.
fn checked_product(self) -> Option<Result>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, for consistency with Iterator, this trait should have no parameters, and the method should be parameterized checked_product<P: CheckedProduct<Self::Item>>.

I think then we could also combine your two iterator extensions into one CheckedIterator trait, or even NumIterator to leave room for more extension methods.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another great idea, thankyou. I have done this and put both into NumIter

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than a new iter module here, I think it would make more sense to move this to the num-iter crate.

I wasn't sure. I'm very happy to close this pull request and move the code changes over there if that's best.

…um traits.

Added NumIter and a blanket implementation
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

Successfully merging this pull request may close these issues.

Add CheckedSum and CheckedProduct traits
2 participants