Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions src/FsToolkit.ErrorHandling/Option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ module Option =
/// <param name="opt">The <c>option</c> to map over.</param>
/// <returns>A <c>Task&lt;'T option&gt;</c> with the mapped value.</returns>
let inline traverseTask
([<InlineIfLambda>] f: 'T -> Task<'T>)
([<InlineIfLambda>] f: 'T -> Task<'U>)
(opt: Option<'T>)
: Task<Option<'T>> =
: Task<Option<'U>> =
sequenceTask ((map f) opt)

/// <summary>
Expand Down Expand Up @@ -409,9 +409,9 @@ module Option =
/// <param name="opt">The Option to map over.</param>
/// <returns>An Async Option with the mapped value.</returns>
let inline traverseAsync
([<InlineIfLambda>] f: 'T -> Async<'T>)
([<InlineIfLambda>] f: 'T -> Async<'U>)
(opt: Option<'T>)
: Async<Option<'T>> =
: Async<Option<'U>> =
sequenceAsync ((map f) opt)

/// <summary>
Expand Down
31 changes: 31 additions & 0 deletions tests/FsToolkit.ErrorHandling.Tests/Option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ let traverseTaskTests =
Expect.equal value (Some "foo") ""
}

testCaseTask "traverseTask allows mapping to new type"
<| fun () ->
task {
let optTask = Some 100

let optFunc =
string
>> Task.singleton

let! value =
(optFunc, optTask)
||> Option.traverseTask

Expect.equal value (Some "100") ""
}

testCaseTask "traverseTask returns None if None"
<| fun () ->
task {
Expand Down Expand Up @@ -439,6 +455,21 @@ let traverseAsyncTests =
Expect.equal value (Some "foo") ""
}

testCaseAsync "traverseAsync allows mapping to different types"
<| async {
let optAsync = Some 100

let optFunc =
(fun i -> string i)
>> Async.singleton

let! value =
(optFunc, optAsync)
||> Option.traverseAsync

Expect.equal value (Some "100") ""
}

testCaseAsync "traverseAsync returns None if None"
<| async {
let optAsync = None
Expand Down
Loading