Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd Iterator::flatten #48115
Conversation
Centril
added
the
T-libs
label
Feb 10, 2018
Centril
requested a review
from
bluss
Feb 10, 2018
rust-highfive
assigned
dtolnay
Feb 10, 2018
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
You should use a new-type because it affords us more wiggle-room /wrt what we need to keep stable. With a regular
and would prohibit us from changing what FlatMap<_> is. |
This comment has been minimized.
This comment has been minimized.
One of my original ideas was to use a newtype, exactly with your rationale, but on the other hand, users may now have to Is there a particular reason you think we'd change the data definition of |
This comment has been minimized.
This comment has been minimized.
|
One reason to use a newtype instead could be to get rid of the type parameter With a type alias, we can't do anything with the On the other hand... having one parameter means that we can't 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... |
This comment has been minimized.
This comment has been minimized.
|
No new struct would be awesome for the code reuse benefits you have mentioned, but still, we'd prefer to have each of 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
removed their request for review
Feb 10, 2018
This comment has been minimized.
This comment has been minimized.
|
@bluss One newtype coming Right Up™ =) On the |
theotherphil
reviewed
Feb 11, 2018
| /// .map(|s| s.chars()) | ||
| /// .flatten() | ||
| /// .collect(); | ||
| /// assert_eq!(merged, "alphabetagamma"); |
This comment has been minimized.
This comment has been minimized.
theotherphil
Feb 11, 2018
Contributor
It's worth mentioning that this is equivalent to flat_map. (And that flat_map is preferable in this case?)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
So... I ran into a problem... If we want to have one type parameter on 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 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 This I think leaves us with two viable paths to take:
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
added
the
S-waiting-on-review
label
Feb 12, 2018
This comment has been minimized.
This comment has been minimized.
|
I discussed this further with @eddyb, and I think the best way forward is to have I'll fix this up in a bit =) |
Centril
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Feb 13, 2018
Centril
changed the title
Add Iterator::flatten + redefine .flat_map(f) = .map(f).flatten()
Add Iterator::flatten
Feb 14, 2018
Centril
added
S-waiting-on-review
and removed
S-waiting-on-author
labels
Feb 14, 2018
This comment has been minimized.
This comment has been minimized.
|
Refactored according to recent discussion and added requested docs by @theotherphil. |
rust-highfive
assigned
alexcrichton
and unassigned
dtolnay
Feb 14, 2018
This comment has been minimized.
This comment has been minimized.
|
Looks good to me, thanks @Centril! Want to open an issue for tracking the instability here and we can r+? |
kennytm
added
S-waiting-on-author
and removed
S-waiting-on-review
labels
Feb 14, 2018
Centril
referenced this pull request
Feb 14, 2018
Closed
Tracking issue for Iterator::flatten #48213
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Done =) Tracking issue is #48213. |
This comment has been minimized.
This comment has been minimized.
|
@bors: r+ Thanks! |
This comment has been minimized.
This comment has been minimized.
|
|
bors
removed
the
S-waiting-on-author
label
Feb 14, 2018
Centril
added some commits
Feb 14, 2018
Centril
force-pushed the
Centril:feature/iterator_flatten
branch
from
a8c93db
to
819d57a
Feb 20, 2018
This comment has been minimized.
This comment has been minimized.
|
Should be up to snuff now. |
This comment has been minimized.
This comment has been minimized.
|
@bors: r+ |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
bors
added
S-waiting-on-bors
and removed
S-waiting-on-author
labels
Feb 24, 2018
bors
added a commit
that referenced
this pull request
Feb 25, 2018
bors
added a commit
that referenced
this pull request
Feb 25, 2018
bors
added a commit
that referenced
this pull request
Feb 25, 2018
kennytm
added a commit
to kennytm/rust
that referenced
this pull request
Feb 25, 2018
bors
added a commit
that referenced
this pull request
Feb 25, 2018
bors
added a commit
that referenced
this pull request
Feb 25, 2018
bors
merged commit 819d57a
into
rust-lang:master
Feb 25, 2018
1 check passed
Centril
deleted the
Centril:feature/iterator_flatten
branch
Feb 25, 2018
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
yrashk
commented
Feb 26, 2018
|
I agree, it would have been nice to at least have better data and maybe a
discussion on this.
…On Feb 26, 2018 3:12 PM, "Christopher Serr" ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#48115 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAABxABUkgPl_Mt1iRb7xK8uj1YSLZ8Wks5tYmdbgaJpZM4SA1Bh>
.
|
kennytm
referenced this pull request
Feb 26, 2018
Merged
Lower the priority of unstable methods when picking a candidate. #48552
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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 There are two ways to work around compatibility problems, mentioned in this itertools PR: bluss/rust-itertools#266, including a free function called |
Centril commentedFeb 10, 2018
•
edited
Tracking issue: #48213
This adds the trait method
.flatten()onIteratorwhich 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 ofFlattenis essentially that of what it was forFlatMapbut removing the call tofat 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