Skip to content
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
14 changes: 7 additions & 7 deletions src/FsToolkit.ErrorHandling/Option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,17 @@ module Option =
}

/// <summary>
/// Maps a <c>Task</c> function over an <c>option</c>, returning a <c>Task&lt;'T option&gt;</c><br/>
/// Maps a <c>Task</c> function over an <c>option</c>, returning a <c>Task&lt;'U option&gt;</c><br/>
///
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/traversetask</href>
/// </summary>
/// <param name="f">The function to map over the <c>option</c>.</param>
/// <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>
/// <returns>A <c>Task&lt;'U 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 @@ -407,11 +407,11 @@ module Option =
/// </summary>
/// <param name="f">The function to map over the Option.</param>
/// <param name="opt">The Option to map over.</param>
/// <returns>An Async Option with the mapped value.</returns>
/// <returns>An <c>Async&lt;Option&lt;'U&gt;&gt;</c> 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