Make Zero and One extensional traits#122
Open
qryxip wants to merge 4 commits intorust-lang-ja:masterfrom
Open
Conversation
Collaborator
|
Will you need to implement |
Member
Author
|
Yes - instead of struct S(i32);
impl Add for S {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Sum for S {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self(0), Add::add)
// ^^^^^^^
// additive identity here
}
}// or:
struct S(i32);
//impl Add for S {
// type Output = Self;
//
// fn add(self, rhs: Self) -> Self::Output {
// Self(self.0 + rhs.0)
// }
//}
impl Sum for S {
// only used for `Zero` from ac-library-rs
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(Self(0), |_, _| unreachable!())
// ^^^^^^^
// additive identity here
}
}
|
Member
Author
|
↑ The second code example was not necessary. |
Collaborator
|
I don't think it is a good idea. "If you want to use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes
ZeroandOneextensional traits forSumandProduct, respectively.As I said in rust-jp.slack.com 3 years ago IIRC, we might as well regard the "zero value" and "one value" described in the documentation as identity elements.
std::iter::Iterator::sumstd::iter::Iterator::productThe implementations should be still inlined.
Affects #102.