-
Notifications
You must be signed in to change notification settings - Fork 67
Add partitionResults with single-pass destructuring #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheAngryByrd
merged 8 commits into
demystifyfp:master
from
bpe-incom:feat/add-partion-with-destructuring
Apr 21, 2026
+222
−0
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1272ae6
Add partitionResult
bpe-incom 6b0d733
Use single-pass implementations for partitionResults
bpe-incom 74c0dc6
Implement suggested changes
bpe-incom f63f3b9
Add null guards
bpe-incom 8f26f0a
rename variable
bpe-incom 3f7fb90
Add docs to gitbook
bpe-incom 1a8def8
ReferenceEquals as null guard and missing xml docs
bpe-incom b964cc3
Add Array section to Gitbook
bpe-incom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Array.partitionResults | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| ## Function Signature | ||
|
|
||
| ```fsharp | ||
| Result<'ok, 'error>[] -> 'ok[] * 'error[] | ||
| ``` | ||
|
|
||
| Separates an array of `Result` values into a tuple of the `Ok` values and the `Error` values, preserving order within each group. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let results = [| Ok 1; Error "bad"; Ok 2; Ok 3; Error "worse" |] | ||
|
|
||
| results |> Array.partitionResults | ||
| // ([| 1; 2; 3 |], [| "bad"; "worse" |]) | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| // string -> Result<int, string> | ||
| let tryParseInt str = | ||
| match System.Int32.TryParse str with | ||
| | true, x -> Ok x | ||
| | false, _ -> Error (sprintf "unable to parse '%s' to integer" str) | ||
|
|
||
| [| "1"; "foo"; "3"; "bar" |] | ||
| |> Array.map tryParseInt | ||
| |> Array.partitionResults | ||
| // ([| 1; 3 |], [| "unable to parse 'foo' to integer"; "unable to parse 'bar' to integer" |]) | ||
| ``` | ||
|
|
||
| ### Example 3 | ||
|
|
||
| All Ok values: | ||
|
|
||
| ```fsharp | ||
| [| Ok 1; Ok 2; Ok 3 |] |> Array.partitionResults | ||
| // ([| 1; 2; 3 |], [||]) | ||
| ``` | ||
|
|
||
| All Error values: | ||
|
|
||
| ```fsharp | ||
| [| Error "a"; Error "b" |] |> Array.partitionResults | ||
| // ([||], [| "a"; "b" |]) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # List.partitionResults | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| ## Function Signature | ||
|
|
||
| ```fsharp | ||
| Result<'ok, 'error> list -> 'ok list * 'error list | ||
| ``` | ||
|
|
||
| Separates a list of `Result` values into a tuple of the `Ok` values and the `Error` values, preserving order within each group. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let results = [Ok 1; Error "bad"; Ok 2; Ok 3; Error "worse"] | ||
|
|
||
| results |> List.partitionResults | ||
| // ([1; 2; 3], ["bad"; "worse"]) | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| // string -> Result<int, string> | ||
| let tryParseInt str = | ||
| match System.Int32.TryParse str with | ||
| | true, x -> Ok x | ||
| | false, _ -> Error (sprintf "unable to parse '%s' to integer" str) | ||
|
|
||
| ["1"; "foo"; "3"; "bar"] | ||
| |> List.map tryParseInt | ||
| |> List.partitionResults | ||
| // ([1; 3], ["unable to parse 'foo' to integer"; "unable to parse 'bar' to integer"]) | ||
| ``` | ||
|
|
||
| ### Example 3 | ||
|
|
||
| All Ok values: | ||
|
|
||
| ```fsharp | ||
| [Ok 1; Ok 2; Ok 3] |> List.partitionResults | ||
| // ([1; 2; 3], []) | ||
| ``` | ||
|
|
||
| All Error values: | ||
|
|
||
| ```fsharp | ||
| [Error "a"; Error "b"] |> List.partitionResults | ||
| // ([], ["a"; "b"]) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Seq.partitionResults | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| ## Function Signature | ||
|
|
||
| ```fsharp | ||
| seq<Result<'ok, 'error>> -> 'ok[] * 'error[] | ||
| ``` | ||
|
|
||
| Separates a sequence of `Result` values into a tuple of the `Ok` values and the `Error` values as arrays, preserving order within each group. | ||
|
|
||
| See also [Array.partitionResults](../array/partitionResults.md). | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let results = seq { Ok 1; Error "bad"; Ok 2; Ok 3; Error "worse" } | ||
|
|
||
| results |> Seq.partitionResults | ||
| // ([| 1; 2; 3 |], [| "bad"; "worse" |]) | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| // string -> Result<int, string> | ||
| let tryParseInt str = | ||
| match System.Int32.TryParse str with | ||
| | true, x -> Ok x | ||
| | false, _ -> Error $"unable to parse '{str}' to integer" | ||
|
|
||
| ["1"; "foo"; "3"; "bar"] | ||
| |> Seq.map tryParseInt | ||
| |> Seq.partitionResults | ||
| // ([| 1; 3 |], [| "unable to parse 'foo' to integer"; "unable to parse 'bar' to integer" |]) | ||
| ``` | ||
|
|
||
| ### Example 3 | ||
|
|
||
| All Ok values: | ||
|
|
||
| ```fsharp | ||
| seq { Ok 1; Ok 2; Ok 3 } |> Seq.partitionResults | ||
| // ([| 1; 2; 3 |], [||]) | ||
| ``` | ||
|
|
||
| All Error values: | ||
|
|
||
| ```fsharp | ||
| seq { Error "a"; Error "b" } |> Seq.partitionResults | ||
| // ([||], [| "a"; "b" |]) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.