Rust: Tracking issue for iterator_fold_self

Created on 11 Jan 2020  路  9Comments  路  Source: rust-lang/rust

Initial implementation: #65222

Tracking issue for Iterator::fold_first, like fold1 in Haskell.

Open questions

  • [ ] Naming. Do we like fold_first?
  • [ ] Should we reconsider try_fold_first?

Stabilisation checklist

  • [ ] Docs
  • [ ] Stabilisation PR
A-iterators B-unstable C-tracking-issue Libs-Small Libs-Tracked T-libs

Most helpful comment

I prefer reduce because it quite different from fold

fold compute acc with all elements then return acc type

but reduce compute all elements into one then return element type

All 9 comments

For me as a Haskeller who relatively new to Rust, try_fold_first sounds right, because it returns an Option.

Following Scala's approach, what about reduce?

Kotlin is doing the same naming.

https://kotlinlang.org/docs/reference/collection-aggregate.html#fold-and-reduce

I prefer reduce because it quite different from fold

fold compute acc with all elements then return acc type

but reduce compute all elements into one then return element type

reduce might be conflict with ParallelIterator::reduce in Rayon, which has the identity parameter act as fold's init parameter.

Why only Iterator::fold_first was implemented and not DoubleEndedIterator::rfold_first? Is it appropriate to make a PR that adds the later method?

I think reduce is an appropriate name for this function. try_fold_first would also be better than fold_first.

This functionality is very useful for situations where there's no reasonable default value. For example, I'm using it for a parser, and would be very interested in it being stabilized.

I don't have a strong opinion on the name.

I concur, this would be very useful regardless of the name.

Was this page helpful?
0 / 5 - 0 ratings