Skip to content

Tracking issue Iterator map_while #68537

Closed

Description

@Mark-Simulacrum

Added in #66577.

Open questions:

  • Implement DoubleEndedIterator, FusedIterator
  • Debug output

Activity

  1. added
    T-libs-api
    C-tracking-issue
    on Jan 25, 2020
  2. added
    A-iterators
    B-unstable
    on Jan 25, 2020
  3. WaffleLapkin commented on Jan 31, 2020

    @WaffleLapkin
    Member

    @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.

  4. Boscop commented on Feb 26, 2020

    @Boscop

    @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?

  5. WaffleLapkin commented on Feb 26, 2020

    @WaffleLapkin
    Member

    @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

    1. we can't change TakeWhile (this would be perfect, but it's breaking change)
    2. 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 })
  6. added a commit that references this issue on Mar 22, 2020
  7. camsteffen commented on May 28, 2020

    @camsteffen
    Contributor

    Ready to stabilize?

  8. 197g commented on Jun 28, 2020

    @197g
    Contributor

    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.

  9. WaffleLapkin commented on Jun 28, 2020

    @WaffleLapkin
    Member

    Wow, I haven't thought that there is such a similar adapter...

    @HeroicKatora please note that after #68820 MapWhile 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).

  10. WaffleLapkin commented on Jun 28, 2020

    @WaffleLapkin
    Member

    Oh, by the way, @Mark-Simulacrum , as #68820 is merged, could you remove current "open questions" from the issue?

    Open questions:

    • Implement DoubleEndedIterator, FusedIterator
    • Debug output
  11. Boscop commented on Jun 29, 2020

    @Boscop

    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.

  12. added
    Libs-Small
    I-libs-radar
    on Jul 29, 2020
  13. 7 remaining items

  14. rfcbot commented on Sep 8, 2021

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  15. added
    final-comment-period
    and removed
    proposed-final-comment-period
    on Sep 8, 2021
  16. Stargateur commented on Sep 15, 2021

    @Stargateur
    Contributor

    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.

  17. Stargateur commented on Sep 16, 2021

    @Stargateur
    Contributor
    .take_while(|x| x.is_some())
    .map(|x| x.unwrap());
    

    @kamulos BY the way .take_while(|x| x.is_some()).flatten()

  18. added
    finished-final-comment-period
    and removed
    final-comment-period
    on Sep 18, 2021
  19. rfcbot commented on Sep 18, 2021

    @rfcbot

    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.

  20. added
    to-announce
    on Sep 18, 2021
  21. matklad commented on Sep 19, 2021

    @matklad
    Contributor

    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.

  22. added a commit that references this issue on Sep 21, 2021
  23. removed
    to-announce
    on Sep 24, 2021
  24. m-ou-se commented on Sep 29, 2021

    @m-ou-se
    Member

    Stabilized in #89086.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-iteratorsB-unstableC-tracking-issueI-libs-radarLibs-SmallT-libs-apidisposition-mergefinished-final-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions