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 Iterator::flatten #48115

Merged
merged 5 commits into from
Feb 25, 2018
Merged

Add Iterator::flatten #48115

merged 5 commits into from
Feb 25, 2018

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Feb 10, 2018

Tracking issue: #48213

This adds the trait method .flatten() on Iterator which flattens one level of nesting from an iterator or (into)iterators. The method .flat_fmap(f) is then redefined as .map(f).flatten(). The implementation of Flatten is essentially that of what it was for FlatMap but removing the call to f at various places.

Hopefully the type alias approach should be OK as was indicated / alluded to by @bluss and @eddyb in rust-lang/rfcs#2306 (comment).

cc @scottmcm

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 10, 2018
@Centril Centril requested a review from bluss February 10, 2018 06:38
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @dtolnay (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@nagisa
Copy link
Member

nagisa commented Feb 10, 2018

You should use a new-type because it affords us more wiggle-room /wrt what we need to keep stable.

With a regular type we wouldn't be able to change what FlatMap<T> is ever again, because code like this becomes valid:

let banana: Flatten<Map<_, _>, _> = peach.flat_map(pineapple);

and would prohibit us from changing what FlatMap<_> is.

@Centril
Copy link
Contributor Author

Centril commented Feb 10, 2018

@nagisa

You should use a new-type because it affords us more wiggle-room /wrt what we need to keep stable.

One of my original ideas was to use a newtype, exactly with your rationale, but on the other hand, users may now have to impl MyTrait for 3 types (Flatten, FlatMap, Map) instead of just Flatten and Map... at least that is what @scottmcm convinced me with (and we don't have to delegate in the impl of FlatMap as well..).

Is there a particular reason you think we'd change the data definition of FlatMap (we can still specialize Flatten<Map<_, _>, _>..) to something else? should we expect the unexpected?

@Centril
Copy link
Contributor Author

Centril commented Feb 10, 2018

One reason to use a newtype instead could be to get rid of the type parameter U in Flatten<I, U> so we could just have Flatten<I> which is nicer.

With a type alias, we can't do anything with the U in FlatMap<I, U, F> since we have no were to put it (and the compiler ignores bounds put on type parameters of type aliases) and so we get an "unused type parameter" error. But we can both use PhantomData or constrain F or I with a newtype.

On the other hand... having one parameter means that we can't #[derive(Clone, Debug)] and must write out the impls ourselves (at least that is what happened when I tried it).

There seems to be good reasons to go with either a type alias or newtype - so for me it is a toss up / draw right now...

@bluss
Copy link
Member

bluss commented Feb 10, 2018

No new struct would be awesome for the code reuse benefits you have mentioned, but still, we'd prefer to have each of .flat_map() and .flatten() use their own iterator struct for encapsulation purposes. So, what @nagisa said.

I would like to boldly skip the much-unused double ended case in the flatten iterator (like Itertools does), but that creates an inconsistency that I guess is not so popular(?)

@bluss bluss removed their request for review February 10, 2018 12:35
@Centril
Copy link
Contributor Author

Centril commented Feb 10, 2018

@bluss One newtype coming Right Up™ =)

On the DoubleEndedIterator, I think that may be too bold? Doing .flatten().rev() may be entirely within the realm of possibilities (cc @scottmcm)...(?) And if we skip .rev(), we don't get code reuse also. Too bad we can't specialize based on the need for doing .rev() so we can have our cake and eat it too..

/// .map(|s| s.chars())
/// .flatten()
/// .collect();
/// assert_eq!(merged, "alphabetagamma");
Copy link
Contributor

Choose a reason for hiding this comment

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

It's worth mentioning that this is equivalent to flat_map. (And that flat_map is preferable in this case?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, why not =) Can't hurt.

@Centril
Copy link
Contributor Author

Centril commented Feb 12, 2018

So... I ran into a problem...

If we want to have one type parameter on Flatten, then it has to be:

pub struct Flatten<I: Iterator>
where I::Item: IntoIterator {
    iter: I,
    frontiter: Option<<I::Item as IntoIterator>::IntoIter>,
    backiter: Option<<I::Item as IntoIterator>::IntoIter>,
}

instead of:

pub struct Flatten<I, U> {
    iter: I,
    frontiter: Option<U>,
    backiter: Option<U>,
}

unfortunately, this implies that the struct definition of FlatMap (assuming we want code reuse) must be:

pub struct FlatMap<I, U: IntoIterator, F>
where I: Iterator, F: FnMut(I::Item) -> U {
    iter: Flatten<Map<I, F>>,
}

instead of:

pub struct FlatMap<I, U: IntoIterator, F> {
    iter: Flatten<Map<I, F>, U>,
}

but now we have imposed two additional conditions upon FlatMap<I, U, F>, namely I: Iterator and F: FnMut(I::Item) -> U. That is a requirement in any case if FlatMap is to be an Iterator, but doing this change will likely break people's code and so it is a non-viable path forward. We could also wait for the 2018 epoch, but I think that the two options below are better.

This I think leaves us with two viable paths to take:

  1. FlatMap is not implemented in terms of Flatten<Map<I, F>> in which case we get zero code reuse. The consequence of Flatten having one type parameter <I> is that Clone and Debug can't be derived and we must write impls manually.
  2. Flatten has two type parameters <I, U> instead of just <I> and this lets us define FlatMap<I, U, F> as a newtype over Flatten<Map<I, F>, U>. This gives us some code reuse (we still have to delegate all traits due to the newtype, but no real logic is repeated). Having the extra type parameter U is a bit annoying, but it seems like we can live with that given that itertools also has a U parameter for Flatten.

Here's my experiment... Dumping it for future reference: https://play.rust-lang.org/?gist=3cfcc5742a7fbdcaea163381f7c72951&version=nightly

My preference would be to go with option 2. but I am very much open to suggestions.

@pietroalbini pietroalbini added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 12, 2018
@Centril
Copy link
Contributor Author

Centril commented Feb 12, 2018

I discussed this further with @eddyb, and I think the best way forward is to have FlattenCompat<I, U> as an internal type that provides all the logic, and then just let Flatten<I> and FlatMap<I, U, F> delegate to those... I think this is the best we can get and gets us closest to having our cake and eat it too.

I'll fix this up in a bit =)

@Centril Centril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 13, 2018
@Centril Centril changed the title Add Iterator::flatten + redefine .flat_map(f) = .map(f).flatten() Add Iterator::flatten Feb 14, 2018
@Centril Centril added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 14, 2018
@Centril
Copy link
Contributor Author

Centril commented Feb 14, 2018

Refactored according to recent discussion and added requested docs by @theotherphil.

r? @alexcrichton

@alexcrichton
Copy link
Member

Looks good to me, thanks @Centril! Want to open an issue for tracking the instability here and we can r+?

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 14, 2018
@Centril
Copy link
Contributor Author

Centril commented Feb 14, 2018

@alexcrichton Done =) Tracking issue is #48213.

@alexcrichton
Copy link
Member

@bors: r+

Thanks!

@bors
Copy link
Contributor

bors commented Feb 14, 2018

📌 Commit 1291432 has been approved by alexcrichton

@bors bors removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 14, 2018
@Centril
Copy link
Contributor Author

Centril commented Feb 20, 2018

r? @alexcrichton

Should be up to snuff now.

@alexcrichton
Copy link
Member

@bors: r+

@bors
Copy link
Contributor

bors commented Feb 24, 2018

📌 Commit 819d57a has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Feb 24, 2018

🌲 The tree is currently closed for pull requests below priority 100, this pull request will be tested once the tree is reopened

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 24, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Feb 25, 2018
…=alexcrichton

Add Iterator::flatten

This adds the trait method `.flatten()` on `Iterator` which flattens one level of nesting from an iterator or (into)iterators. The method `.flat_fmap(f)` is then redefined as `.map(f).flatten()`. The implementation of `Flatten` is essentially that of what it was for `FlatMap` but removing the call to `f` at various places.

Hopefully the type alias approach should be OK as was indicated / alluded to by @bluss and @eddyb in rust-lang/rfcs#2306 (comment).

cc @scottmcm
bors added a commit that referenced this pull request Feb 25, 2018
Rollup of 17 pull requests

- Successful merges: #47964, #47970, #48076, #48115, #48166, #48281, #48297, #48302, #48362, #48369, #48489, #48491, #48494, #48517, #48529, #48235, #48330
- Failed merges:
@bors bors merged commit 819d57a into rust-lang:master Feb 25, 2018
@Centril Centril deleted the feature/iterator_flatten branch February 25, 2018 17:50
@yrashk
Copy link

yrashk commented Feb 26, 2018

@CryZe Christopher, I just ran into this issue with itertools, did indeed break my builds on nightly. Annoying, but I am not sure what would have been the best direction here.

@CryZe
Copy link
Contributor

CryZe commented Feb 26, 2018

This should‘ve at least been a crater run and required some more discussion imo. Even without stabilization, this will break stable in 2 cycles.

@yrashk
Copy link

yrashk commented Feb 26, 2018 via email

@kennytm
Copy link
Member

kennytm commented Feb 26, 2018

@CryZe the insta-breakage should be addressed in #48552.

@bluss
Copy link
Member

bluss commented Mar 4, 2018

I'm sorry that I didn't investigate the impact of the itertools compatibility issue before this feature was merged. The explanation for that is simply that this is a day that I have been expecting, when std starts to incorporate some features of itertools.

There are two ways to work around compatibility problems, mentioned in this itertools PR: rust-itertools/itertools#266, including a free function called flatten that is available from itertools 0.7.7. An automatic solution is not yet available.

bors added a commit that referenced this pull request Mar 24, 2018
Lower the priority of unstable methods when picking a candidate.

Previously, when searching for the impl of a method, we do not consider the stability of the impl. This leads to lots of insta-inference-regressions due to method ambiguity when a popular name is chosen. This has happened multiple times in Rust's history e.g.

* `f64::from_bits` #40470
* `Ord::{min, max}` #42496
* `Ord::clamp` #44095 (eventually got reverted due to these breakages)
* `Iterator::flatten` #48115 (recently added)

This PR changes the probing order so that unstable items are considered last. If a stable item is found, the unstable items will not be considered (but a future-incompatible warning will still be emitted), thus allowing stable code continue to function without using qualified names.

Once the unstable feature is stabilized, the ambiguity error will still be emitted, but the user can also use newly stable std methods, while the current situation is that downstream user is forced to update the code without any immediate benefit.

(I hope that we could bring back `Ord::clamp` if this PR is merged.)
Centril added a commit to Centril/rust that referenced this pull request Jun 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.