fsprojects / fsprojects/FSharpPlus
[API Proposal]: Extend the reduce and reduceBack to Collections
Nobody has claimed this yet.
- Dominant language
- F#
- Stars
- 941
- Forks
- 106
- PR merge metrics
- No merged PRs in 30d
Description
Background and motivation
From the requirements point of view: the fold function must provide init, reduce does not require init, which means no longer needs to write boilerplate "match with" code like "extract the first element from generic Collections as init, the remaining elements as sequence, and then pass it to fold".
From a reasonable point of view: list/array/seq all provide native reduce methods, list/array also provides native reduceBack methods. This generic encapsulation is not difficult to implement, and other languages (except fundamental FPs like Haskell) generally provide reduce methods, which is reasonable.
I know there is a reduce function implementation now, but that one is very difficult to use. To be honest, I have no idea how to pass parameters.
API Proposal
let inline reduce (folder: 'State->'T->'State) (foldable: '``Foldable<'T>``) : 'State
let inline reduceBack (folder: 'T->'State->'State) (foldable: '``Foldable<'T>``) : 'State
API Usage
reduce (*) [1; 2; 3; 4; 5] |> should equal 120
reduce (+) ([]: int list) // throws an exception
Alternative Designs
No response
Risks
No response
Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?
Yes, please assign this issue to me.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the existing reduce function implementation and the 41-comment discussion to understand the unresolved API concerns. Compare the proposed reduce and reduceBack signatures with the native list, array, and seq operations. Done means an agreed design and proof-of-concept covering generic collections and empty-input behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp
- Domain
- developer-experience
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100