Skip to content

Add partitionResults with single-pass destructuring#359

Merged
TheAngryByrd merged 8 commits intodemystifyfp:masterfrom
bpe-incom:feat/add-partion-with-destructuring
Apr 21, 2026
Merged

Add partitionResults with single-pass destructuring#359
TheAngryByrd merged 8 commits intodemystifyfp:masterfrom
bpe-incom:feat/add-partion-with-destructuring

Conversation

@bpe-incom
Copy link
Copy Markdown
Contributor

@bpe-incom bpe-incom commented Apr 13, 2026

Proposed Changes

Adds partitionResults functions to Array, List, and Seq modules that partition a collection of Result<'ok, 'error> into a tuple of unwrapped ok values and error values.

Closes #358

Implementation

All implementations use single-pass traversal as suggested in the issue discussion:

  • Array: Uses ResizeArray accumulators (per @njlr's suggestion)
  • List: Uses tail-recursive loop with cons/rev (idiomatic for F# lists)
  • Seq: Uses ResizeArray accumulators, returns seq rather than array

bpe-incom and others added 2 commits April 9, 2026 08:16
Address review feedback from demystifyfp#358: replace two-pass Array.choose
implementations with single-pass alternatives. Array uses ResizeArray,
List uses tail-recursive loop, Seq returns seq instead of array.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/FsToolkit.ErrorHandling/Seq.fs Outdated
/// </summary>
/// <param name="xs">The input sequence of results.</param>
/// <returns>A tuple where the first element is an array of all Ok values and the second is an array of all Error values.</returns>
let partitionResults (xs: SeqNull<Result<'ok, 'error>>) : 'ok seq * 'error seq =
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is prior art in this repo for the Seq variant yielding arrays. There is no real benefit to downcasting here as it's hard to conceive of a different internal impl that beats accumulating and materializing

So my suggestion here would be for Seq.X to delegate to and Array.X to share this impl. On the array side, let the inputs be Arrays so it could be optimized to e.g. consider the input lengths. On the Seq side you can't but returning arrays lets people pattern match and/or lenght check the arrays immediately vs pretending its lazy and/or them upcasting again

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found it: #310
one notable thing is that ArrayCollector can be useful in this space but in the context of this lib stick with ResizeArray so it can be supported in the Fable impl (which did not have ArrayCollector at least at that time)

Comment thread src/FsToolkit.ErrorHandling/List.fs Outdated
Comment thread src/FsToolkit.ErrorHandling/Array.fs
Comment thread src/FsToolkit.ErrorHandling/Seq.fs Outdated
Copy link
Copy Markdown
Collaborator

@TheAngryByrd TheAngryByrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! Looks good with two outstanding things:

  1. Couple nitpicks for XML docs
  2. Add corresponding docs over in https://github.com/demystifyfp/FsToolkit.ErrorHandling/tree/master/gitbook

(also thanks for all the review work @bartelink!)

Comment thread src/FsToolkit.ErrorHandling/Array.fs
Comment thread src/FsToolkit.ErrorHandling/List.fs
@bpe-incom bpe-incom requested a review from TheAngryByrd April 14, 2026 05:44
Comment thread gitbook/SUMMARY.md
* [sequenceResultM](seq/sequenceResultM.md)
* [traverseResultA](seq/traverseResultA.md)
* [sequenceResultA](seq/sequenceResultA.md)
* [partitionResults](seq/partitionResults.md)
Copy link
Copy Markdown
Contributor

@bartelink bartelink Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't see array ref'd here - seed a new section if required...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we could do that, but it is already just repeats of the same docs for lists and seq where an array section simply would be another permutation.

As a user of the library, I have never 'missed' the array documentation. But we can add it in this commit if @TheAngryByrd would find it useful?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah create a new section please. Don't have to backfill anything that's missing, just reference anything that already exists.

Copy link
Copy Markdown
Collaborator

@TheAngryByrd TheAngryByrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the work!

@TheAngryByrd TheAngryByrd merged commit a7119ca into demystifyfp:master Apr 21, 2026
26 checks passed
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.

Question/Feature: partition collections of results into ok cases and error cases.

3 participants