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

Make AdditiveIterator and MultiplicativeIterator extensible #23293

Merged
merged 2 commits into from
Apr 8, 2015

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Mar 11, 2015

Previously it could not be implemented for types outside libcore/iter.rs due
to coherence issues.

@rust-highfive
Copy link
Collaborator

r? @huonw

(rust_highfive has picked a reviewer for you, use r? to override)

@huonw
Copy link
Member

huonw commented Mar 11, 2015

I suspect this change means that sum and product can become methods on just Iterator.

That said, there's deeper structure here: both of these consumers are operating on a monoid: an algebraic structure consisting an (associative) binary operator and an identity element for that operator. Other examples include appending strings/sequences/data structures in general. I idly wonder if approaching this concept from that more perspective may be better, instead of having two essentially identical traits. sum and product could then become thin wrappers around the monoid version. That said, we probably don't want to be so general in std right now.

Thoughts, @aturon?

@aturon
Copy link
Member

aturon commented Mar 12, 2015

@tbu- I'm curious what coherence problems you were running into? I presume trying to add a blanket impl of some kind?

We used to have something along the lines @huonw is suggesting, where these worked for types satisfying Add + One or Mul + Zero, but the One and Zero traits were removed as part of moving all aspects of a numeric hierarchy out of std for now. That's also why the traits being modified in this PR are unstable.

@tbu-
Copy link
Contributor Author

tbu- commented Mar 12, 2015

@aturon The problem is that AdditiveIterator needs to be implemented for all iterators of a certain item type – you can't do that though because the Iterator and AdditiveIterator are foreign.

@tbu-
Copy link
Contributor Author

tbu- commented Mar 30, 2015

@huonw Any updates on this one?

@SSheldon
Copy link
Contributor

@tbu-, with #23549 reintroducing Zero and One, you can probably remove your AdditiveIteratorItem and MultiplicativeIteratorItem

@bors
Copy link
Contributor

bors commented Apr 1, 2015

☔ The latest upstream changes (presumably #23936) made this pull request unmergeable. Please resolve the merge conflicts.

@SSheldon
Copy link
Contributor

SSheldon commented Apr 4, 2015

@tbu-, what do you think of moving these methods onto the Iterator trait where Item: Add + Zero / Mul + One?

@huonw
Copy link
Member

huonw commented Apr 6, 2015

r? @aturon (transferring reviewership, don't have the bandwidth right now.)

@rust-highfive rust-highfive assigned aturon and unassigned huonw Apr 6, 2015
Previously it could not be implemented for types outside `libcore/iter.rs` due
to coherence issues.
@tbu- tbu- force-pushed the pr_additive_multiplicative branch from 515c878 to 52bb3f7 Compare April 6, 2015 15:45
@tbu-
Copy link
Contributor Author

tbu- commented Apr 6, 2015

@aturon I made sum and product inherent methods now, should be fine to merge.

@aturon
Copy link
Member

aturon commented Apr 6, 2015

@bors: r+

Thanks, @tbu-!

@bors
Copy link
Contributor

bors commented Apr 6, 2015

📌 Commit 52bb3f7 has been approved by aturon

/// assert!(it.sum() == 15);
/// ```
#[unstable(feature="core")]
fn sum<T, S=T>(self) -> S where
Copy link
Member

Choose a reason for hiding this comment

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

Could this also omit the T type parameter? I think something like this in theory shouldn't ICE:

fn sum<S=Self::Item>(self) -> S
    where S: Add<Self::Item,Output=S> + Zero, Self: Sized
{
     ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexcrichton Indeed. Changing it right now.

@alexcrichton
Copy link
Member

Nice!

@tbu- tbu- force-pushed the pr_additive_multiplicative branch from 52bb3f7 to 4b8a918 Compare April 6, 2015 17:00
/// ```
#[unstable(feature="core")]
fn sum<S=<Self as Iterator>::Item>(self) -> S where
S: Add<Self::Item, Output=S> + Zero,
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the output of Add need to be the same as the Item of the iterator? Could this be: fn sum(self) -> <Self::Item as Add>::Output where Self::Item: Add, Self::Item::Output: Zero

Not sure it's that worth it, but it'd allow summing when the output and input are different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The S=... part is just setting a default for the type parameter. Iterator type and type that is being summed over can be different in the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

But the Output=S part requires that the iterator's Item and the Output of Add be the same

Copy link
Contributor

Choose a reason for hiding this comment

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

Ohhh I understand it now :) Disregard!

@alexcrichton
Copy link
Member

@bors: r+ 4b8a918

@bors
Copy link
Contributor

bors commented Apr 6, 2015

⌛ Testing commit 4b8a918 with merge 82d1a5e...

@bors
Copy link
Contributor

bors commented Apr 6, 2015

💔 Test failed - auto-mac-32-opt

In addition to being nicer, this also allows you to use `sum` and `product` for
iterators yielding custom types aside from the standard integers.

Due to removing the `AdditiveIterator` and `MultiplicativeIterator` trait, this
is a breaking change.

[breaking-change]
@tbu-
Copy link
Contributor Author

tbu- commented Apr 7, 2015

@alexcrichton Sorry for the inconvenience, make checked now.

@tbu- tbu- force-pushed the pr_additive_multiplicative branch from 4b8a918 to 97f24a8 Compare April 7, 2015 22:27
@alexcrichton
Copy link
Member

@bors: r+ 97f24a8

No worries!

bors added a commit that referenced this pull request Apr 8, 2015
Previously it could not be implemented for types outside `libcore/iter.rs` due
to coherence issues.
@bors
Copy link
Contributor

bors commented Apr 8, 2015

⌛ Testing commit 97f24a8 with merge dd6c4a8...

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.

7 participants