Use try_fold
instead of fold(Some(...), )
#746
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I used
.fold(Some(...), ...)
pattern three times because it was used inpermutations.rs
(where I started a series of changes) but it leads to a lot of unnecessary.and_then
if we haveNone
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 nochecked_sum
/checked_product
. Related to #745After looking at
Itertools::fold_while
, I found out thatIterator::try_fold
is the right method for the job.PS: I was gonna suggest to deprecate
Itertools::fold_while
in favor ofIterator::try_fold
but I see there was quite some discussion about this in #469. Not sure to understand the whole thing though.