Skip to content

Commit ec0eb22

Browse files
committed
Refactors orElse/orElseWith to not use CE
1 parent 028e72e commit ec0eb22

File tree

5 files changed

+18
-42
lines changed

5 files changed

+18
-42
lines changed

src/FsToolkit.ErrorHandling.JobResult/JobResult.fs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ module JobResult =
7777
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
7878
/// </returns>
7979
let inline orElse (ifError : Job<Result<'ok,'error2>>) (result : Job<Result<'ok,'error>>) =
80-
job {
81-
match! result with
82-
| Ok r -> return Ok r
83-
| Error _ -> return! ifError
84-
}
80+
result
81+
|> Job.bind(Result.either ok (fun _ -> ifError))
8582

8683
/// <summary>
8784
/// Returns <paramref name="result"/> if it is <c>Ok</c>, otherwise executes <paramref name="ifErrorFunc"/> and returns the result.
@@ -103,11 +100,8 @@ module JobResult =
103100
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
104101
/// </returns>
105102
let inline orElseWith (ifErrorFunc : 'error -> Job<Result<'ok,'error2>>) (result : Job<Result<'ok,'error>>) =
106-
job {
107-
match! result with
108-
| Ok r -> return Ok r
109-
| Error e -> return! ifErrorFunc e
110-
}
103+
result
104+
|> Job.bind(Result.either ok ifErrorFunc)
111105

112106
/// Replaces the wrapped value with unit
113107
let ignore jr =

src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ module TaskResult =
7171
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
7272
/// </returns>
7373
let inline orElse (ifError : Task<Result<'ok,'error2>>) (result : Task<Result<'ok,'error>>) =
74-
task {
75-
match! result with
76-
| Ok r -> return Ok r
77-
| Error _ -> return! ifError
78-
}
74+
result
75+
|> Task.bind(Result.either ok (fun _ -> ifError))
7976

8077
/// <summary>
8178
/// Returns <paramref name="result"/> if it is <c>Ok</c>, otherwise executes <paramref name="ifErrorFunc"/> and returns the result.
@@ -97,11 +94,8 @@ module TaskResult =
9794
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
9895
/// </returns>
9996
let inline orElseWith (ifErrorFunc : 'error -> Task<Result<'ok,'error2>>) (result : Task<Result<'ok,'error>>) =
100-
task {
101-
match! result with
102-
| Ok r -> return Ok r
103-
| Error e -> return! ifErrorFunc e
104-
}
97+
result
98+
|> Task.bind(Result.either ok ifErrorFunc)
10599

106100
/// Replaces the wrapped value with unit
107101
let ignore tr =

src/FsToolkit.ErrorHandling/AsyncResult.fs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ module AsyncResult =
8080
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
8181
/// </returns>
8282
let inline orElse (ifError : Async<Result<'ok,'error2>>) (result : Async<Result<'ok,'error>>) =
83-
async {
84-
match! result with
85-
| Ok r -> return Ok r
86-
| Error _ -> return! ifError
87-
}
83+
result
84+
|> Async.bind(Result.either ok (fun _ -> ifError))
8885

8986
/// <summary>
9087
/// Returns <paramref name="result"/> if it is <c>Ok</c>, otherwise executes <paramref name="ifErrorFunc"/> and returns the result.
@@ -106,11 +103,8 @@ module AsyncResult =
106103
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
107104
/// </returns>
108105
let inline orElseWith (ifErrorFunc : 'error -> Async<Result<'ok,'error2>>) (result : Async<Result<'ok,'error>>) =
109-
async {
110-
match! result with
111-
| Ok r -> return Ok r
112-
| Error e -> return! ifErrorFunc e
113-
}
106+
result
107+
|> Async.bind(Result.either ok ifErrorFunc)
114108

115109
/// Replaces the wrapped value with unit
116110
let ignore ar =

src/FsToolkit.ErrorHandling/Result.fs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ module Result =
6565
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
6666
/// </returns>
6767
let inline orElse (ifError : Result<'ok,'error2>) (result : Result<'ok,'error>) =
68-
match result with
69-
| Ok r -> Ok r
70-
| Error _ -> ifError
68+
result |> either Ok (fun _ -> ifError)
7169

7270

7371
/// <summary>
@@ -90,9 +88,7 @@ module Result =
9088
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
9189
/// </returns>
9290
let inline orElseWith (ifErrorFunc : 'error -> Result<'ok,'error2>) (result : Result<'ok,'error>) =
93-
match result with
94-
| Ok r -> Ok r
95-
| Error e -> ifErrorFunc e
91+
result |> either Ok ifErrorFunc
9692

9793
/// Replaces the wrapped value with unit
9894
let ignore result =

src/FsToolkit.ErrorHandling/Validation.fs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ module Validation =
4545
/// The result if the result is Ok, else returns <paramref name="ifError"/>.
4646
/// </returns>
4747
let inline orElse (ifError : Validation<'ok,'error2>) (result : Validation<'ok,'error>) : Validation<'ok,'error2> =
48-
match result with
49-
| Ok r -> Ok r
50-
| Error _ -> ifError
48+
result |> Result.either ok (fun _ -> ifError)
49+
5150

5251

5352
/// <summary>
@@ -70,9 +69,8 @@ module Validation =
7069
/// The result if the result is Ok, else the result of executing <paramref name="ifErrorFunc"/>.
7170
/// </returns>
7271
let inline orElseWith (ifErrorFunc : 'error list -> Validation<'ok,'error2>) (result : Validation<'ok,'error>) : Validation<'ok,'error2> =
73-
match result with
74-
| Ok r -> Ok r
75-
| Error e -> ifErrorFunc e
72+
result |> Result.either ok ifErrorFunc
73+
7674

7775
let map f (x : Validation<_,_>) : Validation<_,_>= Result.map f x
7876

0 commit comments

Comments
 (0)