Skip to content

Commit 2f234ba

Browse files
committed
docs: sequenceResultM
1 parent f23dd0e commit 2f234ba

File tree

4 files changed

+79
-5
lines changed

4 files changed

+79
-5
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 4.16.0
2+
- [refactor!: Seq.sequenceResultM returns Array instead of seq](https://github.com/demystifyfp/FsToolkit.ErrorHandling/pull/255) [@bartelink](https://github.com/bartelink)
3+
14
### 4.15.1 - January 15, 2024
25
- [Doc updates](https://github.com/demystifyfp/FsToolkit.ErrorHandling/pull/247) Credits @1eyewonder
36

gitbook/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* [sequenceResultM](list/sequenceResultM.md)
2626
* [traverseResultA](list/traverseResultA.md)
2727
* [sequenceResultA](list/sequenceResultA.md)
28+
* Seqs
29+
* [sequenceResultM](seq/sequenceResultM.md)
2830
* Transforms
2931
* [ofChoice](result/ofChoice.md)
3032

gitbook/seq/sequenceResultM.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Seq.sequenceResultM
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 monadic, stopping on the first error. Compare the example below with [sequenceResultA](sequenceResultA.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.sequenceResultM
29+
// Ok [| 1; 2; 3 |]
30+
31+
seq { "1"; "foo"; "3"; "bar" }
32+
|> Seq.map tryParseInt
33+
|> Seq.sequenceResultM
34+
// Error "unable to parse 'foo' to integer"
35+
```
36+
37+
### Example 2
38+
39+
```fsharp
40+
// int -> Result<bool, string>
41+
let isPrime (x: int) =
42+
if x < 2 then Error $"{x} must be greater than 1"
43+
elif x = 2 then Ok true
44+
else
45+
let rec isPrime' (x : int) (i : int) =
46+
if i * i > x then Ok true
47+
elif x % i = 0 then Ok false
48+
else isPrime' x (i + 1)
49+
isPrime' x 2
50+
51+
// int seq -> Result<bool, string list>
52+
let checkIfAllPrime (numbers: seq<int>) =
53+
numbers
54+
|> Seq.map isPrime // Result<bool, string> list
55+
|> Seq.sequenceResultM // Result<bool list, string>
56+
|> Result.map (Array.forall id) // shortened version of '|> Result.map (fun bools -> bools |> 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" |]
63+
64+
let a = seq { 2; 3; 4; 5 } |> checkIfAllPrime
65+
// Ok false
66+
67+
let a = [2; 3; 5;] |> checkIfAllPrime
68+
// Ok true
69+
```

tests/FsToolkit.ErrorHandling.Tests/Seq.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ open Fable.Mocha
99
#if !FABLE_COMPILER
1010
open Expecto
1111
#endif
12+
open FsToolkit.ErrorHandling
1213
open SampleDomain
1314
open TestData
14-
open FsToolkit.ErrorHandling
1515

1616
let sequenceResultMTests =
1717
testList "Seq.sequenceResultM Tests" [
18-
testCase "sequenceResult with an empty sequence"
18+
testCase "empty sequence"
1919
<| fun _ ->
2020
let tweets = Seq.empty
2121
let expected = Ok [||]
@@ -24,7 +24,7 @@ let sequenceResultMTests =
2424

2525
Expect.equal actual expected "Should have an empty list of valid tweets"
2626

27-
testCase "sequenceResult with a sequence of valid data"
27+
testCase "valid data"
2828
<| fun _ ->
2929
let tweets =
3030
seq {
@@ -39,7 +39,7 @@ let sequenceResultMTests =
3939

4040
Expect.equal actual expected "Should have a list of valid tweets"
4141

42-
testCase "sequenceResult with few invalid data"
42+
testCase "valid and invalid data"
4343
<| fun _ ->
4444
let tweets =
4545
seq {
@@ -54,7 +54,7 @@ let sequenceResultMTests =
5454

5555
Expect.equal actual expected "traverse the sequence and return the first error"
5656

57-
testCase "sequenceResult stops after first invalid data"
57+
testCase "stops after first invalid data"
5858
<| fun _ ->
5959
let mutable counter = 0
6060

0 commit comments

Comments
 (0)