You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A-iteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Reacted by Boscop, Cameron Steffen, Stanislav Tkach, Pim van den Berg, sloshwoven, Cecile Tonglet, Jakub Alba, Josh Triplett, Phillip Samuel K., Jakub Beránek and 1 more
Reacted by Boscop, Maciej Hirsz, Michael, Karsten Nikolai Strand, Billie Thompson, jgarvin, Arif Roktim, porkbrain, Cecile Tonglet, dantepippi and 3 more
@ollie27 noticed in #66577 (comment) that MapWhile doesn't need to be FusedIterator (and so we can remove the finished flag). However TakeWhile is already FusedIterator and we can't change that since it would be a breaking change. It may be confusing to users that MapWhile and TakeWhile works a bit differently.
How do we choose? Remove the finished flag and probably confuse some users or make less flexible implementation?
Personally I would prefer removing the finished flag, but I'm not sure.
@WaffleLapkin So the reasoning that it shouldn't impl FusedIterator is that the user might want to continue consuming this iterator after it first returned None (e.g. find multiple "sections" in the original iterator), right?
Then I think removing the finished flag is the right thing to do.
Btw, since TakeWhile impls FusedIterator shouldn't we also want to add a version of it that isn't fused?
@Boscop Yes, one of the reasons is that user might want to continue. But it is a rare case, so it's not so important.
In my opinion, we need to remove the finished flag because there is no need for it to exist. When you need to guarantee Nones after the first None, you must call .fuse() (there is even a note in the docs that says that you should prefer .fuse() over T: FusedIterator) and in most cases, you don't need to worry because everything (for loop, fold, for_each, collect...) stops on the first None. That way MapWhile is just more zero-cost.
Not sure if we need unfused TakeWhile since
we can't change TakeWhile (this would be perfect, but it's breaking change)
all cases of unfused TakeWhile are covered by MapWhile. E.g. .unfused_take_while(pred) == .map_while(|it| if pred(&it) { Some(it) } else { None })
This method overall is very, very similar to scan.
// These two yield the same items.
iter.map_while(predicate)
iter.scan((), |(), item| predicate(item)).fuse()
Is there any impl (unsafe or not) that could be added to scan+fuse but not to scan and fuse alone by composition? This would be one possible way to distinguish these two methods, otherwise I see little point in adding map_while here instead of itertools.
Wow, I haven't thought that there is such a similar adapter...
@HeroicKatora please note that after #68820MapWhile does not implement FusedIteraror and thus, iter.map_while(predicate) is analog to just iter.scan((), |(), item| predicate(item)).
However, I'm still in favour of adding map_while to std, because (in my opinion) there are a lot of situations where map_while is more readable than scan (()s are a bit nasty) and also scan doesn't come to mind in such situations (nobody has mentioned it for 7 months after the original PR :D).
Reacted by Boscop, Arif Roktim, Nano and Aurelia Molzer
I agree with @WaffleLapkin that the scan solution is very non-obvious and also harder to read for others who read the code. So I'd really prefer to use map_while.
The impl of the map_while method should maybe be written using the scan solution.
This method overall is very, very similar to scan.
// These two yield the same items.
iter.map_while(predicate)
iter.scan((), |(), item| predicate(item)).fuse()
Is there any impl (unsafe or not) that could be added to scan+fuse but not to scan and fuse alone by composition? This would be one possible way to distinguish these two methods, otherwise I see little point in adding map_while here instead of itertools.
scan can do everything... that not a good argument in this context.
This method overall is very, very similar to scan.
scan and map_while are equivalent:
scan(init, |state, item| exr)
// is equivalent to
let state = &mut init;
map_whie(|item| expr)
map_while even is more general, as you can get the final state after the iteration is finished.
So, I'd say map_while is what we should have had from the begging, and scan is just an overly complicated version of that with an extra state argument, which makes sense in a purely functional language, but is unnecessary in Rust.
Reacted by Mara Bos
Reacted by Aurelia Molzer, waffle, Jane Losare-Lusby and Zac Scott
A-iteratorsArea: IteratorsB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.
Activity
WaffleLapkin commentedon Jan 31, 2020
@ollie27 noticed in #66577 (comment) that
MapWhiledoesn't need to beFusedIterator(and so we can remove thefinishedflag). HoweverTakeWhileis alreadyFusedIteratorand we can't change that since it would be a breaking change. It may be confusing to users thatMapWhileandTakeWhileworks a bit differently.How do we choose? Remove the
finishedflag and probably confuse some users or make less flexible implementation?Personally I would prefer removing the
finishedflag, but I'm not sure.Boscop commentedon Feb 26, 2020
@WaffleLapkin So the reasoning that it shouldn't impl
FusedIteratoris that the user might want to continue consuming this iterator after it first returnedNone(e.g. find multiple "sections" in the original iterator), right?Then I think removing the
finishedflag is the right thing to do.Btw, since
TakeWhileimplsFusedIteratorshouldn't we also want to add a version of it that isn't fused?WaffleLapkin commentedon Feb 26, 2020
@Boscop Yes, one of the reasons is that user might want to continue. But it is a rare case, so it's not so important.
In my opinion, we need to remove the
finishedflag because there is no need for it to exist. When you need to guaranteeNones after the firstNone, you must call.fuse()(there is even a note in the docs that says that you should prefer.fuse()overT: FusedIterator) and in most cases, you don't need to worry because everything (forloop,fold,for_each,collect...) stops on the firstNone. That wayMapWhileis just more zero-cost.Not sure if we need unfused
TakeWhilesinceTakeWhile(this would be perfect, but it's breaking change)TakeWhileare covered byMapWhile. E.g..unfused_take_while(pred)==.map_while(|it| if pred(&it) { Some(it) } else { None })Auto merge of rust-lang#68820 - WaffleLapkin:remove_finished_from_map…
camsteffen commentedon May 28, 2020
Ready to stabilize?
197g commentedon Jun 28, 2020
This method overall is very, very similar to
scan.Is there any impl (
unsafeor not) that could be added toscan+fusebut not to scan and fuse alone by composition? This would be one possible way to distinguish these two methods, otherwise I see little point in addingmap_whilehere instead of itertools.WaffleLapkin commentedon Jun 28, 2020
Wow, I haven't thought that there is such a similar adapter...
@HeroicKatora please note that after #68820
MapWhiledoes not implementFusedIterarorand thus,iter.map_while(predicate)is analog to justiter.scan((), |(), item| predicate(item)).However, I'm still in favour of adding
map_whiletostd, because (in my opinion) there are a lot of situations wheremap_whileis more readable thanscan(()s are a bit nasty) and also scan doesn't come to mind in such situations (nobody has mentioned it for 7 months after the original PR :D).WaffleLapkin commentedon Jun 28, 2020
Oh, by the way, @Mark-Simulacrum , as #68820 is merged, could you remove current "open questions" from the issue?
Boscop commentedon Jun 29, 2020
I agree with @WaffleLapkin that the
scansolution is very non-obvious and also harder to read for others who read the code. So I'd really prefer to usemap_while.The impl of the
map_whilemethod should maybe be written using thescansolution.7 remaining items
rfcbot commentedon Sep 8, 2021
🔔 This is now entering its final comment period, as per the review above. 🔔
Stargateur commentedon Sep 15, 2021
scancan do everything... that not a good argument in this context.Stargateur commentedon Sep 16, 2021
@kamulos BY the way
.take_while(|x| x.is_some()).flatten()rfcbot commentedon Sep 18, 2021
The final comment period, with a disposition to merge, as per the review above, is now complete.
As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.
The RFC will be merged soon.
matklad commentedon Sep 19, 2021
scanandmap_whileare equivalent:map_whileeven is more general, as you can get the final state after the iteration is finished.So, I'd say
map_whileis what we should have had from the begging, andscanis just an overly complicated version of that with an extrastateargument, which makes sense in a purely functional language, but is unnecessary in Rust.Rollup merge of rust-lang#89086 - WaffleLapkin:stabilize_iter_map_whi…
m-ou-se commentedon Sep 29, 2021
Stabilized in #89086.