Skip to content

Commit ade8e70

Browse files
committed
Fixing List.traverseResultA issues
1 parent ed43d87 commit ade8e70

File tree

7 files changed

+264
-265
lines changed

7 files changed

+264
-265
lines changed

FsToolkit.ErrorHandling.sln

Lines changed: 233 additions & 233 deletions
Large diffs are not rendered by default.

src/FsToolkit.ErrorHandling.JobResult/JobResult.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ module JobResult =
2121
let inline foldResult ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
2222
Job.map (Result.fold onSuccess onError) jr
2323

24+
let inline eitherMap ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) jr =
25+
Job.map (Result.eitherMap onSuccess onError) jr
26+
2427
let inline ofAsync aAsync =
2528
aAsync
2629
|> Job.fromAsync

src/FsToolkit.ErrorHandling.JobResult/List.fs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@ module List =
2727
let sequenceJobResultM xs = traverseJobResultM id xs
2828

2929

30-
let rec private traverseJobResultA' state f xs =
30+
let rec private traverseJobResultA' state (f : _ -> Job<Result<_,_>>) xs =
3131
match xs with
3232
| [] ->
3333
state
34-
|> JobResult.map List.rev
34+
|> JobResult.eitherMap List.rev List.rev
3535
| x :: xs -> job {
3636
let! s = state
3737

38-
let! fR =
39-
f x
40-
|> JobResult.mapError List.singleton
38+
let! fR = f x
39+
4140

4241
match s, fR with
4342
| Ok ys, Ok y -> return! traverseJobResultA' (JobResult.retn (y :: ys)) f xs
4443
| Error errs, Error e ->
45-
return! traverseJobResultA' (JobResult.returnError (errs @ e)) f xs
46-
| Ok _, Error e
44+
return! traverseJobResultA' (JobResult.returnError (e :: errs)) f xs
45+
| Ok _, Error e -> return! traverseJobResultA' (JobResult.returnError [e]) f xs
4746
| Error e, Ok _ -> return! traverseJobResultA' (JobResult.returnError e) f xs
4847
}
4948

src/FsToolkit.ErrorHandling.TaskResult/Task.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ module Task =
1212
value
1313
|> Task.FromResult
1414

15-
let inline bind ([<InlineIfLambda>] f: 'a -> Task<'b>) (x: Task<'a>) = task {
16-
let! x = x
17-
return! f x
18-
}
15+
let inline bind ([<InlineIfLambda>] f: 'a -> Task<'b>) (x: Task<'a>) =
16+
task {
17+
let! x = x
18+
return! f x
19+
}
1920

2021
let inline bindV ([<InlineIfLambda>] f: 'a -> Task<'b>) (x: ValueTask<'a>) = task {
2122
let! x = x

src/FsToolkit.ErrorHandling/AsyncResult.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ module AsyncResult =
4343
: Async<'output> =
4444
Async.map (Result.either onSuccess onError) input
4545

46+
let inline eitherMap ([<InlineIfLambda>] onSuccess) ([<InlineIfLambda>] onError) input =
47+
Async.map (Result.eitherMap onSuccess onError) input
48+
4649
#if !FABLE_COMPILER
4750

4851
let inline ofTask (aTask: Task<'ok>) : Async<Result<'ok, exn>> =

src/FsToolkit.ErrorHandling/List.fs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,38 +51,30 @@ module List =
5151

5252

5353
let rec private traverseResultA' state f xs =
54-
match xs with
55-
| [] ->
56-
state
57-
|> Result.map List.rev
58-
| x :: xs ->
59-
let fR =
60-
f x
61-
|> Result.mapError List.singleton
54+
match xs with
55+
| [] -> state |> Result.eitherMap List.rev List.rev
56+
| x :: xs ->
6257

63-
match state, fR with
64-
| Ok ys, Ok y -> traverseResultA' (Ok(y :: ys)) f xs
65-
| Error errs, Error e -> traverseResultA' (Error(errs @ e)) f xs
66-
| Ok _, Error e
67-
| Error e, Ok _ -> traverseResultA' (Error e) f xs
58+
match state, f x with
59+
| Ok ys, Ok y -> traverseResultA' (Ok(y :: ys)) f xs
60+
| Error errs, Error e -> traverseResultA' (Error(e :: errs)) f xs
61+
| Ok _, Error e -> traverseResultA' (Error [e]) f xs
62+
| Error e, Ok _ -> traverseResultA' (Error e) f xs
6863

6964
let rec private traverseAsyncResultA' state f xs =
7065
match xs with
7166
| [] ->
7267
state
73-
|> AsyncResult.map List.rev
68+
|> AsyncResult.eitherMap List.rev List.rev
69+
7470
| x :: xs -> async {
7571
let! s = state
76-
77-
let! fR =
78-
f x
79-
|> AsyncResult.mapError List.singleton
80-
72+
let! fR = f x
8173
match s, fR with
8274
| Ok ys, Ok y -> return! traverseAsyncResultA' (AsyncResult.retn (y :: ys)) f xs
8375
| Error errs, Error e ->
84-
return! traverseAsyncResultA' (AsyncResult.returnError (errs @ e)) f xs
85-
| Ok _, Error e
76+
return! traverseAsyncResultA' (AsyncResult.returnError (e :: errs)) f xs
77+
| Ok _, Error e -> return! traverseAsyncResultA' (AsyncResult.returnError [e]) f xs
8678
| Error e, Ok _ -> return! traverseAsyncResultA' (AsyncResult.returnError e) f xs
8779
}
8880

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ open System.Threading.Tasks
1111
open FSharp.Control.Tasks
1212
#endif
1313

14+
1415
[<Tests>]
1516
let ``TaskResultCE return Tests`` =
1617
testList "TaskResultCE Tests" [

0 commit comments

Comments
 (0)