Skip to content
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

Use try_fold instead of fold(Some(...), ) #746

Merged
merged 4 commits into from
Sep 6, 2023

Conversation

Philippe-Cholet
Copy link
Member

I used .fold(Some(...), ...) pattern three times because it was used in permutations.rs (where I started a series of changes) but it leads to a lot of unnecessary .and_then if we have None early.
I previously thought it could be better replaced by something like .map(...).sum::<Option<_>>() with MSRV 1.37+ but no because it does not prevent the underlying operation to overflow. And there is no checked_sum/checked_product. Related to #745
After looking at Itertools::fold_while, I found out that Iterator::try_fold is the right method for the job.

PS: I was gonna suggest to deprecate Itertools::fold_while in favor of Iterator::try_fold but I see there was quite some discussion about this in #469. Not sure to understand the whole thing though.

@jswrenn jswrenn added this pull request to the merge queue Sep 6, 2023
Merged via the queue into rust-itertools:master with commit 58a25fb Sep 6, 2023
9 checks passed
@Philippe-Cholet Philippe-Cholet deleted the try-fold branch September 6, 2023 16:20
@jswrenn jswrenn mentioned this pull request Nov 14, 2023
@jswrenn jswrenn added this to the next milestone Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants