Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 1.05 KB

File metadata and controls

53 lines (37 loc) · 1.05 KB

Array.partitionResults

Namespace: FsToolkit.ErrorHandling

Function Signature

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

let results = [| Ok 1; Error "bad"; Ok 2; Ok 3; Error "worse" |]

results |> Array.partitionResults
// ([| 1; 2; 3 |], [| "bad"; "worse" |])

Example 2

// 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:

[| Ok 1; Ok 2; Ok 3 |] |> Array.partitionResults
// ([| 1; 2; 3 |], [||])

All Error values:

[| Error "a"; Error "b" |] |> Array.partitionResults
// ([||], [| "a"; "b" |])