Skip to content

Commit edbaa5b

Browse files
committed
Option.traverseAsync and traverseTask now allow mapping to new types
1 parent feedaae commit edbaa5b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/FsToolkit.ErrorHandling/Option.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ module Option =
347347
/// <param name="opt">The <c>option</c> to map over.</param>
348348
/// <returns>A <c>Task&lt;'T option&gt;</c> with the mapped value.</returns>
349349
let inline traverseTask
350-
([<InlineIfLambda>] f: 'T -> Task<'T>)
350+
([<InlineIfLambda>] f: 'T -> Task<'U>)
351351
(opt: Option<'T>)
352-
: Task<Option<'T>> =
352+
: Task<Option<'U>> =
353353
sequenceTask ((map f) opt)
354354

355355
/// <summary>
@@ -409,9 +409,9 @@ module Option =
409409
/// <param name="opt">The Option to map over.</param>
410410
/// <returns>An Async Option with the mapped value.</returns>
411411
let inline traverseAsync
412-
([<InlineIfLambda>] f: 'T -> Async<'T>)
412+
([<InlineIfLambda>] f: 'T -> Async<'U>)
413413
(opt: Option<'T>)
414-
: Async<Option<'T>> =
414+
: Async<Option<'U>> =
415415
sequenceAsync ((map f) opt)
416416

417417
/// <summary>

tests/FsToolkit.ErrorHandling.Tests/Option.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ let traverseTaskTests =
275275
Expect.equal value (Some "foo") ""
276276
}
277277

278+
testCaseTask "traverseTask allows mapping to new type"
279+
<| fun () ->
280+
task {
281+
let optTask = Some 100
282+
let optFunc =
283+
string
284+
>> Task.singleton
285+
let! value =
286+
(optFunc, optTask)
287+
||> Option.traverseTask
288+
Expect.equal value (Some "100") ""
289+
}
290+
278291
testCaseTask "traverseTask returns None if None"
279292
<| fun () ->
280293
task {
@@ -439,6 +452,18 @@ let traverseAsyncTests =
439452
Expect.equal value (Some "foo") ""
440453
}
441454

455+
testCaseAsync "traverseAsync allows mapping to different types"
456+
<| async {
457+
let optAsync = Some 100
458+
let optFunc =
459+
(fun i -> string i)
460+
>> Async.singleton
461+
let! value =
462+
(optFunc, optAsync)
463+
||> Option.traverseAsync
464+
Expect.equal value (Some "100") ""
465+
}
466+
442467
testCaseAsync "traverseAsync returns None if None"
443468
<| async {
444469
let optAsync = None

0 commit comments

Comments
 (0)