-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594
Comments
Is this usable to implement a fold1? |
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. |
That same technique is also used for the |
I think |
Well, |
I wound up writing some code that really wanted |
Add Iterator::try_for_each The fallible version of `for_each` aka the stateless version of `try_fold`. Inspired by @cuviper's comment in rust-lang#45379 (comment) as a more direct and obvious solution than `.map(f).collect::<Result<(), _>>()`. Like `for_each`, no need for an `r` version thanks to overrides in `Rev`. `iterator_try_fold` tracking issue: rust-lang#45594
Should we try to stabilize |
Why not try_fold into |
Add Iterator::try_for_each The fallible version of `for_each` aka the stateless version of `try_fold`. Inspired by @cuviper's comment in rust-lang#45379 (comment) as a more direct and obvious solution than `.map(f).collect::<Result<(), _>>()`. Like `for_each`, no need for an `r` version thanks to overrides in `Rev`. `iterator_try_fold` tracking issue: rust-lang#45594
In a perfect world the two signatures would be equivalent, but the code that landed already uses the formulation that works better with inference so I think it’s better to keep it that way.
Let’s stabilize them all together? :) @rfcbot fcp merge As of today |
@SimonSapin Looks like the rfcbot command didn't take. Does it need to be the first line? |
@scottmcm The issue was missing a team tag :). @SimonSapin If you try again it should enter FCP (I cannot, as I am not on the libs team). |
Let’s try again: @rfcbot fcp merge |
Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
To be clear, this is stabilizing before the This completely slipped by me even though I desperately wish for it all the time, as I never would've figured that functions involving Try would be receiving stabilization yet. I hope we've gained enough experience using these to validate their utility? Note: I know this is not unheard of; consider the |
Yes, this is stabilizing before the That doesn't scare me on the flexibility side because, no matter how the On the consumption side, I'm not worried because this is basically the same as itertools's They've also been discussed for a while as core internal iteration methods, though those wins are only available to stdlib types until the Now, one could argue that |
The final comment period is now complete. |
…chton Stabilize iterator methods in 1.27 - Closes rust-lang#39480, feature `iter_rfind` - `DoubleEndedIterator::rfind` - Closes rust-lang#44705, feature `iter_rfold` - `DoubleEndedIterator::rfold` - Closes rust-lang#45594, feature `iterator_try_fold` - `Iterator::try_fold` - `Iterator::try_for_each` - `DoubleEndedIterator::try_rfold`
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 offor_each
or the stateless version oftry_fold
.https://doc.rust-lang.org/nightly/std/iter/trait.Iterator.html#method.try_for_each
Pre-stabilization question list:
try_foreach
as well? impl FromIterator<()> for () #45379 (comment)rfold
beforetry_rfold
? Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594 (comment)try_fold
before the others? Tracking issue for Iterator::try_fold and try_rfold (feature iterator_try_fold) #45594 (comment)The text was updated successfully, but these errors were encountered: