Skip to content

Commit ac33c85

Browse files
authored
Fixed #195 (#199)
* Remove Result.fold This wasnt' really fold and FSharp.Core implemented it * Fixed defaultWith to use error in error case
1 parent ab98b1d commit ac33c85

File tree

8 files changed

+12
-91
lines changed

8 files changed

+12
-91
lines changed

src/FsToolkit.ErrorHandling.JobResult/JobResult.fs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ module JobResult =
1818
>> Job.result))
1919
jr
2020

21-
let inline foldResult ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
22-
Job.map (Result.fold onSuccess onError) jr
23-
2421
let inline eitherMap ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
2522
Job.map (Result.eitherMap onSuccess onError) jr
2623

@@ -186,7 +183,7 @@ module JobResult =
186183

187184
/// Extracts the contained value of an job-wrapped result if Ok, otherwise
188185
/// evaluates ifErrorThunk and uses the result.
189-
let inline defaultWith ifErrorThunk jobResult =
186+
let inline defaultWith ([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok) jobResult =
190187
jobResult
191188
|> Job.map (Result.defaultWith ifErrorThunk)
192189

src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ module TaskResult =
2121
>> Task.singleton)
2222
)
2323

24-
let inline foldResult ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) tr =
25-
Task.map (Result.fold onSuccess onError) tr
26-
2724
let inline ofAsync aAsync =
2825
aAsync
2926
|> Async.Catch

src/FsToolkit.ErrorHandling/AsyncResult.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ module AsyncResult =
244244
/// Extracts the contained value of an async-wrapped result if Ok, otherwise
245245
/// evaluates ifErrorThunk and uses the result.
246246
let inline defaultWith
247-
([<InlineIfLambda>] ifErrorThunk: unit -> 'ok)
247+
([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok)
248248
(asyncResult: Async<Result<'ok, 'error>>)
249249
: Async<'ok> =
250250
asyncResult

src/FsToolkit.ErrorHandling/Result.fs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,6 @@ module Result =
8787
| _, Error e, _
8888
| _, _, Error e -> Error e
8989

90-
let inline fold
91-
([<InlineIfLambda>] onOk: 'okInput -> 'output)
92-
([<InlineIfLambda>] onError: 'errorInput -> 'output)
93-
(input: Result<'okInput, 'errorInput>)
94-
: 'output =
95-
match input with
96-
| Ok x -> onOk x
97-
| Error err -> onError err
98-
9990
let inline ofChoice (input: Choice<'ok, 'error>) : Result<'ok, 'error> =
10091
match input with
10192
| Choice1Of2 x -> Ok x
@@ -252,12 +243,12 @@ module Result =
252243
/// Returns the contained value if Ok, otherwise evaluates ifErrorThunk and
253244
/// returns the result.
254245
let inline defaultWith
255-
([<InlineIfLambda>] ifErrorThunk: unit -> 'ok)
246+
([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok)
256247
(result: Result<'ok, 'error>)
257248
: 'ok =
258249
match result with
259250
| Ok x -> x
260-
| Error _ -> ifErrorThunk ()
251+
| Error e -> ifErrorThunk e
261252

262253
/// Same as defaultValue for a result where the Ok value is unit. The name
263254
/// describes better what is actually happening in this case.

tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,6 @@ let map2Tests =
9494
]
9595

9696

97-
[<Tests>]
98-
let foldResultTests =
99-
100-
testList "JobResult.foldResult tests" [
101-
testCase "foldResult with Task(Ok x)"
102-
<| fun _ ->
103-
createPostSuccess validCreatePostRequest
104-
|> JobResult.foldResult (fun (PostId id) -> id.ToString()) string
105-
|> runJobSync
106-
|> Expect.same (newPostId.ToString())
107-
108-
testCase "foldResult with Task(Error x)"
109-
<| fun _ ->
110-
createPostFailure validCreatePostRequest
111-
|> JobResult.foldResult string (fun ex -> ex.Message)
112-
|> runJobSync
113-
|> Expect.same (commonEx.Message)
114-
]
115-
116-
11797
[<Tests>]
11898
let mapErrorTests =
11999
testList "JobResult.mapError tests" [
@@ -467,13 +447,13 @@ let defaultWithTests =
467447
testList "JobResult.defaultWith Tests" [
468448
testCase "defaultWith returns the ok value"
469449
<| fun _ ->
470-
let v = JobResult.defaultWith (fun () -> 43) (toJob (Ok 42))
450+
let v = JobResult.defaultWith (fun _ -> 43) (toJob (Ok 42))
471451

472452
Expect.hasJobValue 42 v
473453

474454
testCase "defaultValue invoks the given thunk for Error"
475455
<| fun _ ->
476-
let v = JobResult.defaultWith (fun () -> 42) (toJob (Error err))
456+
let v = JobResult.defaultWith (fun _ -> 42) (toJob (Error err))
477457

478458
Expect.hasJobValue 42 v
479459
]

tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,6 @@ let map2Tests =
9696
|> Expect.hasTaskErrorValueSync getFollowersEx
9797
]
9898

99-
100-
[<Tests>]
101-
let foldResultTests =
102-
103-
testList "TaskResult.foldResult tests" [
104-
testCase "foldResult with Task(Ok x)"
105-
<| fun _ ->
106-
createPostSuccess validCreatePostRequest
107-
|> TaskResult.foldResult (fun (PostId id) -> id.ToString()) string
108-
|> runTaskSync
109-
|> Expect.same (newPostId.ToString())
110-
111-
testCase "foldResult with Task(Error x)"
112-
<| fun _ ->
113-
createPostFailure validCreatePostRequest
114-
|> TaskResult.foldResult string (fun ex -> ex.Message)
115-
|> runTaskSync
116-
|> Expect.same (commonEx.Message)
117-
]
118-
119-
12099
[<Tests>]
121100
let mapErrorTests =
122101
testList "TaskResult.mapError tests" [
@@ -469,13 +448,13 @@ let defaultWithTests =
469448
testList "TaskResult.defaultWith Tests" [
470449
testCase "defaultWith returns the ok value"
471450
<| fun _ ->
472-
let v = TaskResult.defaultWith (fun () -> 43) (toTask (Ok 42))
451+
let v = TaskResult.defaultWith (fun _ -> 43) (toTask (Ok 42))
473452

474453
Expect.hasTaskValue 42 v
475454

476455
testCase "defaultValue invoks the given thunk for Error"
477456
<| fun _ ->
478-
let v = TaskResult.defaultWith (fun () -> 42) (toTask (Error err))
457+
let v = TaskResult.defaultWith (fun _ -> 42) (toTask (Error err))
479458

480459
Expect.hasTaskValue 42 v
481460
]

tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ let defaultErrorTests =
436436
let defaultWithTests =
437437
testList "AsyncResult.defaultWith Tests" [
438438
testCaseAsync "defaultWith returns the ok value"
439-
<| (let v = AsyncResult.defaultWith (fun () -> 43) (toAsync (Ok 42))
439+
<| (let v = AsyncResult.defaultWith (fun _ -> 43) (toAsync (Ok 42))
440440

441441
Expect.hasAsyncValue 42 v)
442442

443443
testCaseAsync "defaultValue invoks the given thunk for Error"
444-
<| (let v = AsyncResult.defaultWith (fun () -> 42) (toAsync (Error err))
444+
<| (let v = AsyncResult.defaultWith (fun _ -> 42) (toAsync (Error err))
445445

446446
Expect.hasAsyncValue 42 v)
447447
]

tests/FsToolkit.ErrorHandling.Tests/Result.fs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,6 @@ let applyTests =
132132
|> Expect.hasErrorValue emptyTweetErrMsg
133133
]
134134

135-
136-
let foldTests =
137-
138-
testList "Result.fold tests" [
139-
testCase "fold with Ok"
140-
<| fun _ ->
141-
let actual =
142-
Tweet.TryCreate "foobar"
143-
|> Result.fold (fun t -> t.Value) id
144-
145-
Expect.equal actual "foobar" "fold to string should succeed"
146-
147-
testCase "fold with Error"
148-
<| fun _ ->
149-
let actual =
150-
Tweet.TryCreate ""
151-
|> Result.fold (fun t -> t.Value) id
152-
153-
Expect.equal actual emptyTweetErrMsg "fold to string should fail"
154-
]
155-
156-
157135
let ofChoiceTests =
158136

159137
testList "Result.ofChoice tests" [
@@ -504,13 +482,13 @@ let defaultWithTests =
504482
testList "defaultWith Tests" [
505483
testCase "defaultWith returns the ok value"
506484
<| fun _ ->
507-
let v = Result.defaultWith (fun () -> 43) (Ok 42)
485+
let v = Result.defaultWith (fun _ -> 43) (Ok 42)
508486

509487
Expect.equal v 42 ""
510488

511489
testCase "defaultValue invoks the given thunk for Error"
512490
<| fun _ ->
513-
let v = Result.defaultWith (fun () -> 42) (Error err)
491+
let v = Result.defaultWith (fun _ -> 42) (Error err)
514492

515493
Expect.equal v 42 ""
516494
]
@@ -821,7 +799,6 @@ let allTests =
821799
map2Tests
822800
map3Tests
823801
applyTests
824-
foldTests
825802
ofChoiceTests
826803
resultCETests
827804
resultOperatorsTests

0 commit comments

Comments
 (0)