Skip to content

Commit

Permalink
Fixed #195 (#199)
Browse files Browse the repository at this point in the history
* Remove Result.fold

This wasnt' really fold and FSharp.Core implemented it

* Fixed defaultWith to use error in error case
  • Loading branch information
TheAngryByrd authored Nov 19, 2022
1 parent ab98b1d commit ac33c85
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 91 deletions.
5 changes: 1 addition & 4 deletions src/FsToolkit.ErrorHandling.JobResult/JobResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ module JobResult =
>> Job.result))
jr

let inline foldResult ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
Job.map (Result.fold onSuccess onError) jr

let inline eitherMap ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
Job.map (Result.eitherMap onSuccess onError) jr

Expand Down Expand Up @@ -186,7 +183,7 @@ module JobResult =

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

Expand Down
3 changes: 0 additions & 3 deletions src/FsToolkit.ErrorHandling.TaskResult/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ module TaskResult =
>> Task.singleton)
)

let inline foldResult ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) tr =
Task.map (Result.fold onSuccess onError) tr

let inline ofAsync aAsync =
aAsync
|> Async.Catch
Expand Down
2 changes: 1 addition & 1 deletion src/FsToolkit.ErrorHandling/AsyncResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ module AsyncResult =
/// Extracts the contained value of an async-wrapped result if Ok, otherwise
/// evaluates ifErrorThunk and uses the result.
let inline defaultWith
([<InlineIfLambda>] ifErrorThunk: unit -> 'ok)
([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok)
(asyncResult: Async<Result<'ok, 'error>>)
: Async<'ok> =
asyncResult
Expand Down
13 changes: 2 additions & 11 deletions src/FsToolkit.ErrorHandling/Result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,6 @@ module Result =
| _, Error e, _
| _, _, Error e -> Error e

let inline fold
([<InlineIfLambda>] onOk: 'okInput -> 'output)
([<InlineIfLambda>] onError: 'errorInput -> 'output)
(input: Result<'okInput, 'errorInput>)
: 'output =
match input with
| Ok x -> onOk x
| Error err -> onError err

let inline ofChoice (input: Choice<'ok, 'error>) : Result<'ok, 'error> =
match input with
| Choice1Of2 x -> Ok x
Expand Down Expand Up @@ -252,12 +243,12 @@ module Result =
/// Returns the contained value if Ok, otherwise evaluates ifErrorThunk and
/// returns the result.
let inline defaultWith
([<InlineIfLambda>] ifErrorThunk: unit -> 'ok)
([<InlineIfLambda>] ifErrorThunk: 'error -> 'ok)
(result: Result<'ok, 'error>)
: 'ok =
match result with
| Ok x -> x
| Error _ -> ifErrorThunk ()
| Error e -> ifErrorThunk e

/// Same as defaultValue for a result where the Ok value is unit. The name
/// describes better what is actually happening in this case.
Expand Down
24 changes: 2 additions & 22 deletions tests/FsToolkit.ErrorHandling.JobResult.Tests/JobResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,6 @@ let map2Tests =
]


[<Tests>]
let foldResultTests =

testList "JobResult.foldResult tests" [
testCase "foldResult with Task(Ok x)"
<| fun _ ->
createPostSuccess validCreatePostRequest
|> JobResult.foldResult (fun (PostId id) -> id.ToString()) string
|> runJobSync
|> Expect.same (newPostId.ToString())

testCase "foldResult with Task(Error x)"
<| fun _ ->
createPostFailure validCreatePostRequest
|> JobResult.foldResult string (fun ex -> ex.Message)
|> runJobSync
|> Expect.same (commonEx.Message)
]


[<Tests>]
let mapErrorTests =
testList "JobResult.mapError tests" [
Expand Down Expand Up @@ -467,13 +447,13 @@ let defaultWithTests =
testList "JobResult.defaultWith Tests" [
testCase "defaultWith returns the ok value"
<| fun _ ->
let v = JobResult.defaultWith (fun () -> 43) (toJob (Ok 42))
let v = JobResult.defaultWith (fun _ -> 43) (toJob (Ok 42))

Expect.hasJobValue 42 v

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

Expect.hasJobValue 42 v
]
Expand Down
25 changes: 2 additions & 23 deletions tests/FsToolkit.ErrorHandling.TaskResult.Tests/TaskResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@ let map2Tests =
|> Expect.hasTaskErrorValueSync getFollowersEx
]


[<Tests>]
let foldResultTests =

testList "TaskResult.foldResult tests" [
testCase "foldResult with Task(Ok x)"
<| fun _ ->
createPostSuccess validCreatePostRequest
|> TaskResult.foldResult (fun (PostId id) -> id.ToString()) string
|> runTaskSync
|> Expect.same (newPostId.ToString())

testCase "foldResult with Task(Error x)"
<| fun _ ->
createPostFailure validCreatePostRequest
|> TaskResult.foldResult string (fun ex -> ex.Message)
|> runTaskSync
|> Expect.same (commonEx.Message)
]


[<Tests>]
let mapErrorTests =
testList "TaskResult.mapError tests" [
Expand Down Expand Up @@ -469,13 +448,13 @@ let defaultWithTests =
testList "TaskResult.defaultWith Tests" [
testCase "defaultWith returns the ok value"
<| fun _ ->
let v = TaskResult.defaultWith (fun () -> 43) (toTask (Ok 42))
let v = TaskResult.defaultWith (fun _ -> 43) (toTask (Ok 42))

Expect.hasTaskValue 42 v

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

Expect.hasTaskValue 42 v
]
Expand Down
4 changes: 2 additions & 2 deletions tests/FsToolkit.ErrorHandling.Tests/AsyncResult.fs
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,12 @@ let defaultErrorTests =
let defaultWithTests =
testList "AsyncResult.defaultWith Tests" [
testCaseAsync "defaultWith returns the ok value"
<| (let v = AsyncResult.defaultWith (fun () -> 43) (toAsync (Ok 42))
<| (let v = AsyncResult.defaultWith (fun _ -> 43) (toAsync (Ok 42))

Expect.hasAsyncValue 42 v)

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

Expect.hasAsyncValue 42 v)
]
Expand Down
27 changes: 2 additions & 25 deletions tests/FsToolkit.ErrorHandling.Tests/Result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,6 @@ let applyTests =
|> Expect.hasErrorValue emptyTweetErrMsg
]


let foldTests =

testList "Result.fold tests" [
testCase "fold with Ok"
<| fun _ ->
let actual =
Tweet.TryCreate "foobar"
|> Result.fold (fun t -> t.Value) id

Expect.equal actual "foobar" "fold to string should succeed"

testCase "fold with Error"
<| fun _ ->
let actual =
Tweet.TryCreate ""
|> Result.fold (fun t -> t.Value) id

Expect.equal actual emptyTweetErrMsg "fold to string should fail"
]


let ofChoiceTests =

testList "Result.ofChoice tests" [
Expand Down Expand Up @@ -504,13 +482,13 @@ let defaultWithTests =
testList "defaultWith Tests" [
testCase "defaultWith returns the ok value"
<| fun _ ->
let v = Result.defaultWith (fun () -> 43) (Ok 42)
let v = Result.defaultWith (fun _ -> 43) (Ok 42)

Expect.equal v 42 ""

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

Expect.equal v 42 ""
]
Expand Down Expand Up @@ -821,7 +799,6 @@ let allTests =
map2Tests
map3Tests
applyTests
foldTests
ofChoiceTests
resultCETests
resultOperatorsTests
Expand Down

0 comments on commit ac33c85

Please sign in to comment.