|
| 1 | +# Seq.sequenceResultA |
| 2 | + |
| 3 | +Namespace: `FsToolkit.ErrorHandling` |
| 4 | + |
| 5 | +## Function Signature |
| 6 | + |
| 7 | +```fsharp |
| 8 | +seq<Result<'a, 'b>> -> Result<'a[], 'b[]> |
| 9 | +``` |
| 10 | + |
| 11 | +This is applicative, collecting all errors. Compare the example below with [sequenceResultM](sequenceResultM.md). |
| 12 | + |
| 13 | +See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | +### Example 1 |
| 18 | + |
| 19 | +```fsharp |
| 20 | +// string -> Result<int, string> |
| 21 | +let tryParseInt str = |
| 22 | + match Int32.TryParse str with |
| 23 | + | true, x -> Ok x |
| 24 | + | false, _ -> Error $"unable to parse '{str}' to integer" |
| 25 | +
|
| 26 | +["1"; "2"; "3"] |
| 27 | +|> Seq.map tryParseInt |
| 28 | +|> Seq.sequenceResultA |
| 29 | +// Ok [| 1; 2; 3 |] |
| 30 | +
|
| 31 | +["1"; "foo"; "3"; "bar"] |
| 32 | +|> Seq.map tryParseInt |
| 33 | +|> Seq.sequenceResultA |
| 34 | +// Error [| "unable to parse 'foo' to integer" |
| 35 | +// "unable to parse 'bar' to integer" |] |
| 36 | +``` |
| 37 | + |
| 38 | +### Example 2 |
| 39 | + |
| 40 | +```fsharp |
| 41 | +// int -> Result<bool, string> |
| 42 | +let isPrime (x: int) = |
| 43 | + if x < 2 then Error $"{x} must be greater than 1" |
| 44 | + elif x = 2 then Ok true |
| 45 | + else |
| 46 | + let rec isPrime' (x : int) (i : int) = |
| 47 | + if i * i > x then Ok true |
| 48 | + elif x % i = 0 then Ok false |
| 49 | + else isPrime' x (i + 1) |
| 50 | + isPrime' x 2 |
| 51 | + |
| 52 | +// seq<int> -> Result<bool, string[]> |
| 53 | +let checkIfAllPrime (numbers: seq<int>) = |
| 54 | + seq { for x in numbers -> isPrime x } // Result<bool, string> seq |
| 55 | + |> Seq.sequenceResultA // Result<bool[], string[]> |
| 56 | + |> Result.map (Seq.forall id) // shortened version of '|> Result.map (fun results -> results |> Array.forall (fun x -> x = true))' |
| 57 | + |
| 58 | +let a = [| 1; 2; 3; 4; 5 |] |> checkIfAllPrime |
| 59 | +// Error [| "1 must be greater than 1" |] |
| 60 | +
|
| 61 | +let b = [ 1; 2; 3; 4; 5; 0 ] |> checkIfAllPrime |
| 62 | +// Error [| "1 must be greater than 1"; "0 must be greater than 1" |] |
| 63 | +
|
| 64 | +let a = seq { 2; 3; 4; 5 } |> checkIfAllPrime |
| 65 | +// Ok false |
| 66 | +
|
| 67 | +let a = seq { 2; 3; 5 } |> checkIfAllPrime |
| 68 | +// Ok true |
| 69 | +``` |
0 commit comments