File tree Expand file tree Collapse file tree 7 files changed +39
-0
lines changed
FsToolkit.ErrorHandling.JobResult
FsToolkit.ErrorHandling.TaskResult Expand file tree Collapse file tree 7 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ ## Validation Computation Expression
2+
3+ Namespace: ` FsToolkit.ErrorHandling `
4+
5+ The ` Validation ` type is defined as:
6+
7+ ``` fsharp
8+ type Validation<'a,'err> = Result<'a, 'err list>
9+
10+
11+ This CE can take advantage of the [and! operator](https://github.com/fsharp/fslang-suggestions/issues/579) to join multiple error results into a list.
12+
13+
14+ ## Examples:
15+
16+ ### Example 1
17+
18+ The example from [Validation.map3](../validation/map3.md#example-1) can be solved using the `validation` computation expression as below:
19+
20+ ```fsharp
21+ // Validation<int, string>
22+ let addResult = validation {
23+ let! x = tryParseInt "35"
24+ and! y = tryParseInt "5"
25+ and! z = tryParseInt "2"
26+ return add x y z
27+ }
28+ ```
29+
Original file line number Diff line number Diff line change @@ -139,6 +139,7 @@ module JobResult =
139139 let teeErrorIf predicate f jobResult =
140140 jobResult |> Job.map ( Result.teeErrorIf predicate f)
141141
142+ /// Takes two results and returns a tuple of the pair
142143 let zip j1 j2 =
143144 Job.zip j1 j2
144145 |> Job.map( fun ( r1 , r2 ) -> Result.zip r1 r2)
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ module Task =
2525 let map3 f x y z =
2626 apply ( map2 f x y) z
2727
28+
29+ /// Takes two tasks and returns a tuple of the pair
2830 let zip ( a1 : Task < _ >) ( a2 : Task < _ >) = task {
2931 let! r1 = a1
3032 let! r2 = a2
Original file line number Diff line number Diff line change @@ -133,6 +133,7 @@ module TaskResult =
133133 let teeErrorIf predicate f taskResult =
134134 taskResult |> Task.map ( Result.teeErrorIf predicate f)
135135
136+ /// Takes two results and returns a tuple of the pair
136137 let zip x1 x2 =
137138 Task.zip x1 x2
138139 |> Task.map( fun ( r1 , r2 ) -> Result.zip r1 r2)
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ module Async =
1919 let map3 f x y z =
2020 apply ( map2 f x y) z
2121
22+
23+ /// Takes two asyncs and returns a tuple of the pair
2224 let zip a1 a2 = async {
2325 let! r1 = a1
2426 let! r2 = a2
Original file line number Diff line number Diff line change @@ -138,6 +138,8 @@ module AsyncResult =
138138 let teeErrorIf predicate f asyncResult =
139139 asyncResult |> Async.map ( Result.teeErrorIf predicate f)
140140
141+
142+ /// Takes two results and returns a tuple of the pair
141143 let zip x1 x2 =
142144 Async.zip x1 x2
143145 |> Async.map( fun ( r1 , r2 ) -> Result.zip r1 r2)
Original file line number Diff line number Diff line change @@ -157,6 +157,7 @@ module Result =
157157 let teeError f result =
158158 teeErrorIf ( fun _ -> true ) f result
159159
160+ /// Converts a Result<Async<_>,_> to an Async<Result<_,_>>
160161 let sequenceAsync ( resAsync : Result < Async < 'a >, 'b >) : Async < Result < 'a , 'b >> =
161162 async {
162163 match resAsync with
@@ -172,6 +173,7 @@ module Result =
172173 | Ok x -> x
173174 | Error x -> f x
174175
176+ /// Takes two results and returns a tuple of the pair
175177 let zip x1 x2 =
176178 match x1, x2 with
177179 | Ok x1res, Ok x2res -> Ok ( x1res, x2res)
You can’t perform that action at this time.
0 commit comments