fsprojects / fsprojects/FSharpPlus

[API Proposal]: Extend the reduce and reduceBack to Collections

Open
#625 41 comments 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.