Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
* [sequenceResultM](list/sequenceResultM.md)
* [traverseResultA](list/traverseResultA.md)
* [sequenceResultA](list/sequenceResultA.md)
* [partitionResults](list/partitionResults.md)
* Sequences
* [traverseResultM](seq/traverseResultM.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.

* Arrays
* [partitionResults](array/partitionResults.md)
* Transforms
* [ofChoice](result/ofChoice.md)

Expand Down
53 changes: 53 additions & 0 deletions gitbook/array/partitionResults.md
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" |])
```
53 changes: 53 additions & 0 deletions gitbook/list/partitionResults.md
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"])
```
55 changes: 55 additions & 0 deletions gitbook/seq/partitionResults.md
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" |])
```
19 changes: 19 additions & 0 deletions src/FsToolkit.ErrorHandling/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,22 @@ module Array =
let sequenceVOptionM xs = traverseVOptionM id xs

#endif

/// <summary>
/// Partitions an array of results into a tuple of the ok values and the error values.
/// </summary>
/// <param name="input">The input array 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 (input: Result<'ok, 'error>[]) : 'ok[] * 'error[] =
Comment thread
bpe-incom marked this conversation as resolved.
if System.Object.ReferenceEquals(input, null) then
nullArg (nameof input)

let oks = ResizeArray()
Comment thread
bpe-incom marked this conversation as resolved.
let errors = ResizeArray()

for x in input do
match x with
| Ok v -> oks.Add v
| Error e -> errors.Add e

oks.ToArray(), errors.ToArray()
19 changes: 19 additions & 0 deletions src/FsToolkit.ErrorHandling/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,22 @@ module List =


#endif

/// <summary>
/// Partitions a list of results into a tuple of the ok values and the error values.
/// </summary>
/// <param name="input">The input list of results.</param>
/// <returns>A tuple where the first element is a list of all Ok values and the second is a list of all Error values.</returns>
let partitionResults (input: Result<'ok, 'error> list) : 'ok list * 'error list =
Comment thread
bpe-incom marked this conversation as resolved.
if System.Object.ReferenceEquals(input, null) then
nullArg (nameof input)

let oks = ResizeArray()
let errors = ResizeArray()

for x in input do
match x with
| Ok v -> oks.Add v
| Error e -> errors.Add e

List.ofSeq oks, List.ofSeq errors
19 changes: 19 additions & 0 deletions src/FsToolkit.ErrorHandling/Seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,22 @@ let traverseVOptionM f xs =
let sequenceVOptionM xs = traverseVOptionM id xs

#endif

/// <summary>
/// Partitions a sequence of results into a tuple of the ok values and the error values.
/// </summary>
/// <param name="input">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 (input: SeqNull<Result<'ok, 'error>>) : 'ok[] * 'error[] =
if isNull input then
nullArg (nameof input)

let oks = ResizeArray()
let errors = ResizeArray()

for x in input do
match x with
| Ok v -> oks.Add v
| Error e -> errors.Add e

oks.ToArray(), errors.ToArray()
Loading