Skip to content

Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594

Closed
@scottmcm

Description

@scottmcm
Member

The core internal iteration methods in terms of which the other Iterator methods can be implemented.

PR #45595, commit b32267f -- merged 2017-11-17
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.try_fold
https://doc.rust-lang.org/nightly/std/iter/trait.DoubleEndedIterator.html#method.try_rfold

Also try_for_each (PR #48157, merged 2018-02-24), a convenience method that can be considered the fallible version of for_each or the stateless version of try_fold.
https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.try_for_each

Pre-stabilization question list:

Activity

leonardo-m

leonardo-m commented on Oct 28, 2017

@leonardo-m
bluss

bluss commented on Oct 28, 2017

@bluss
Member

There's already a fold1 in itertools and its implementation is quite simple:

    fn fold1<F>(mut self, f: F) -> Option<Self::Item>
        where F: FnMut(Self::Item, Self::Item) -> Self::Item,
              Self: Sized,
    {
        self.next().map(move |x| self.fold(x, f))
    }

The major difference to haskell foldl1 is that itertools fold1 returns an Option, with None for when the iterator is empty, instead of a runtime error like in Haskell.

scottmcm

scottmcm commented on Oct 29, 2017

@scottmcm
MemberAuthor

That same technique is also used for the min & max methods on Iterator: https://github.com/rust-lang/rust/pull/45595/files#diff-1398f322bc563592215b583e9b0ba936R2360

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Oct 31, 2017
cuviper

cuviper commented on Nov 8, 2017

@cuviper
Member

I think try_rfold should conceptually depend on stabilizing rfold first -- #44705.
(even though the default implementation goes the other way, rfold calling try_rfold)

scottmcm

scottmcm commented on Nov 8, 2017

@scottmcm
MemberAuthor

Well, rfold is already in the current beta, so it could probably be proposed for stabilization in 1.23...

cramertj

cramertj commented on Jan 4, 2018

@cramertj
Member

I wound up writing some code that really wanted try_filter_map (return a Result<Option<T>> instead of just Option<T>), and instead wound up doing something like .map(|x| x.to_res_opt()).try_filter_map(res_opt_to_opt_res). I wound up opening this PR as a result, which allows you to use Into to get the res_opt_to_opt_res behavior, however I still think built-in try_xxxs are more straightforward (I could see try_filter, try_filter_map, try_scan, try_inspect, etc... all being handy). I'm not sure how far we should take this before we just say to write a dang for loop 😜

added 2 commits that reference this issue on Feb 19, 2018

Rollup merge of rust-lang#48157 - scottmcm:try-for-each, r=dtolnay

ed532f9
cuviper

cuviper commented on Feb 20, 2018

@cuviper
Member

Should we try to stabilize try_fold alone? (leaving try_rfold and try_for_each for later)

SoniEx2

SoniEx2 commented on Feb 23, 2018

@SoniEx2
Contributor

Why not try_fold into Iterator<R>? make it the opposite of flat_map!

19 remaining items

Loading
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

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cuviper@SimonSapin@TimNN@ExpHP@SoniEx2

        Issue actions

          Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) · Issue #45594 · rust-lang/rust