Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added traverse and sequence functions for Option #231

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions src/FsToolkit.ErrorHandling/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module List =

let sequenceAsyncResultM xs = traverseAsyncResultM id xs


let rec private traverseResultA' state f xs =
match xs with
| [] ->
Expand Down Expand Up @@ -115,8 +114,52 @@ module List =

let sequenceValidationA xs = traverseValidationA id xs


let traverseAsyncResultA f xs =
traverseAsyncResultA' (AsyncResult.retn []) f xs

let sequenceAsyncResultA xs = traverseAsyncResultA id xs

let rec private traverseOptionM' (state: Option<_>) (f: _ -> Option<_>) xs =
match xs with
| [] ->
state
|> Option.map List.rev
| x :: xs ->
let r =
option {
let! y = f x
let! ys = state
return y :: ys
}

match r with
| Some _ -> traverseOptionM' r f xs
| None -> r

let rec private traverseAsyncOptionM' (state: Async<Option<_>>) (f: _ -> Async<Option<_>>) xs =
match xs with
| [] ->
state
|> AsyncOption.map List.rev
| x :: xs ->
async {
let! o =
asyncOption {
let! y = f x
let! ys = state
return y :: ys
}

match o with
| Some _ -> return! traverseAsyncOptionM' (Async.singleton o) f xs
| None -> return o
}

let traverseOptionM f xs = traverseOptionM' (Some []) f xs

let sequenceOptionM xs = traverseOptionM id xs

let traverseAsyncOptionM f xs =
traverseAsyncOptionM' (AsyncOption.retn []) f xs

let sequenceAsyncOptionM xs = traverseAsyncOptionM id xs
138 changes: 138 additions & 0 deletions tests/FsToolkit.ErrorHandling.Tests/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ let traverseResultMTests =
"traverse the list and return the first error"
]

let traverseOptionMTests =
testList "List.traverseOptionM Tests" [
let tryTweetOption x =
match x with
| x when String.IsNullOrEmpty x -> None
| _ -> Some x

testCase "traverseOption with a list of valid data"
<| fun _ ->
let tweets = [
"Hi"
"Hello"
"Hola"
]

let expected = Some tweets
let actual = List.traverseOptionM tryTweetOption tweets

Expect.equal actual expected "Should have a list of valid tweets"

testCase "traverseOption with few invalid data"
<| fun _ ->
let tweets = [
"Hi"
"Hello"
String.Empty
]

let expected = None
let actual = List.traverseOptionM tryTweetOption tweets

Expect.equal actual expected "traverse the list and return none"
]

let sequenceResultMTests =
testList "List.sequenceResultM Tests" [
Expand Down Expand Up @@ -82,6 +115,38 @@ let sequenceResultMTests =
"traverse the list and return the first error"
]

let sequenceOptionMTests =
testList "List.sequenceOptionM Tests" [
let tryTweetOption x =
match x with
| x when String.IsNullOrEmpty x -> None
| _ -> Some x

testCase "traverseOption with a list of valid data"
<| fun _ ->
let tweets = [
"Hi"
"Hello"
"Hola"
]

let expected = Some tweets
let actual = List.sequenceOptionM (List.map tryTweetOption tweets)

Expect.equal actual expected "Should have a list of valid tweets"

testCase "sequenceOptionM with few invalid data"
<| fun _ ->
let tweets = [
String.Empty
"Hello"
String.Empty
]

let actual = List.sequenceOptionM (List.map tryTweetOption tweets)

Expect.equal actual None "traverse the list and return none"
]

let traverseResultATests =
testList "List.traverseResultA Tests" [
Expand Down Expand Up @@ -285,6 +350,38 @@ let traverseAsyncResultMTests =
}
]

let traverseAsyncOptionMTests =

let userIds = [
userId1
userId2
userId3
]

testList "List.traverseAsyncOptionM Tests" [
testCaseAsync "traverseAsyncOptionM with a list of valid data"
<| async {
let expected = Some userIds
let f x = async { return Some x }
let actual = List.traverseAsyncOptionM f userIds

match expected with
| Some e -> do! Expect.hasAsyncSomeValue e actual
| None -> failwith "Error in the test case code"
}

testCaseAsync "traverseOptionA with few invalid data"
<| async {
let expected = None
let f _ = async { return None }
let actual = List.traverseAsyncOptionM f userIds

match expected with
| Some _ -> failwith "Error in the test case code"
| None -> do! Expect.hasAsyncNoneValue actual
}
]

let notifyFailure (PostId _) (UserId uId) =
async {
if
Expand Down Expand Up @@ -370,6 +467,43 @@ let sequenceAsyncResultMTests =
}
]

let sequenceAsyncOptionMTests =

let userIds = [
userId1
userId2
userId3
]

testList "List.sequenceAsyncOptionM Tests" [
testCaseAsync "sequenceAsyncOptionM with a list of valid data"
<| async {
let expected = Some userIds
let f x = async { return Some x }

let actual =
List.map f userIds
|> List.sequenceAsyncOptionM

match expected with
| Some e -> do! Expect.hasAsyncSomeValue e actual
| None -> failwith "Error in the test case code"
}

testCaseAsync "sequenceOptionA with few invalid data"
<| async {
let expected = None
let f _ = async { return None }

let actual =
List.map f userIds
|> List.sequenceAsyncOptionM

match expected with
| Some _ -> failwith "Error in the test case code"
| None -> do! Expect.hasAsyncNoneValue actual
}
]

let sequenceAsyncResultATests =
let userIds =
Expand Down Expand Up @@ -412,13 +546,17 @@ let sequenceAsyncResultATests =
let allTests =
testList "List Tests" [
traverseResultMTests
traverseOptionMTests
sequenceResultMTests
sequenceOptionMTests
traverseResultATests
sequenceResultATests
traverseValidationATests
sequenceValidationATests
traverseAsyncResultMTests
traverseAsyncOptionMTests
traverseAsyncResultATests
sequenceAsyncResultMTests
sequenceAsyncOptionMTests
sequenceAsyncResultATests
]