You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceTask](sequenceTask.md).
12
+
13
+
See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/).
14
+
15
+
## Examples
16
+
17
+
### Example 1
18
+
19
+
Let's assume we have a type `Customer`:
20
+
21
+
```fsharp
22
+
type Customer = {
23
+
Id : int
24
+
Email : string
25
+
}
26
+
```
27
+
28
+
And we have a function called `getCustomerByEmail` that retrieves a `Customer` by email address asynchronously from some external source -- a database, a web service, etc:
29
+
30
+
```fsharp
31
+
// string -> Task<Customer>
32
+
let getCustomerByEmail email : Task<Customer> = task {
33
+
return { Id = 1; Email = "[email protected]" } // return a constant for simplicity
34
+
}
35
+
```
36
+
37
+
If we have a value of type `string option` and want to call the `getCustomerByEmail` function, we can achieve it using the `traverseTask` function as below:
Copy file name to clipboardExpand all lines: src/FsToolkit.ErrorHandling/Option.fs
+65Lines changed: 65 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -321,6 +321,71 @@ module Option =
321
321
322
322
opt
323
323
324
+
#if!FABLE_COMPILER
325
+
openSystem.Threading.Tasks
326
+
327
+
/// <summary>
328
+
/// Converts an <c>Option<Task<_>></c> to an <c>Task<Option<_>></c><br />
329
+
///
330
+
/// Documentation is found here: <see href="https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/sequencetask" />
331
+
/// </summary>
332
+
let inlinesequenceTask(optTask:Option<Task<'T>>):Task<Option<'T>>=
333
+
task{
334
+
match optTask with
335
+
| Some tsk ->
336
+
let!x= tsk
337
+
return Some x
338
+
| None ->return None
339
+
}
340
+
341
+
/// <summary>
342
+
/// Maps a <c>Task</c> function over an <c>option</c>, returning a <c>Task<'T option></c><br/>
343
+
///
344
+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/traversetask</href>
345
+
/// </summary>
346
+
/// <param name="f">The function to map over the <c>option</c>.</param>
347
+
/// <param name="opt">The <c>option</c> to map over.</param>
348
+
/// <returns>A <c>Task<'T option></c> with the mapped value.</returns>
349
+
let inlinetraverseTask
350
+
([<InlineIfLambda>]f:'T ->Task<'T>)
351
+
(opt:Option<'T>)
352
+
:Task<Option<'T>>=
353
+
sequenceTask ((map f) opt)
354
+
355
+
/// <summary>
356
+
/// Converts an <c>Async<Result<'ok,'error>> option</c> to an <c>Async<Result<'ok option,'error>></c><br />
357
+
///
358
+
/// Documentation is found here: <see href="https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/sequencetaskresult" />
359
+
/// </summary>
360
+
let inlinesequenceTaskResult
361
+
(optTaskResult:Task<Result<'T, 'E>>option)
362
+
:Task<Result<'T option, 'E>>=
363
+
task{
364
+
match optTaskResult with
365
+
| Some taskRes ->
366
+
let!xRes= taskRes
367
+
368
+
return
369
+
xRes
370
+
|> Result.map Some
371
+
| None ->return Ok None
372
+
}
373
+
374
+
/// <summary>
375
+
/// Maps a <c>TaskResult</c> function over an <c>option</c>, returning a <c>Task<Result<'U option, 'E>></c>.<br />
376
+
///
377
+
/// Documentation is found here: <see href="https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/traversetaskresult" />
378
+
/// </summary>
379
+
/// <param name="f">The function to map over the <c>option</c>.</param>
380
+
/// <param name="opt">The <c>option</c> to map over.</param>
381
+
/// <returns>A <c>Task<Result<'U option, 'E>></c> with the mapped value.</returns>
382
+
let inlinetraverseTaskResult
383
+
([<InlineIfLambda>]f:'T ->Task<Result<'U, 'E>>)
384
+
(opt:'T option)
385
+
:Task<Result<'U option, 'E>>=
386
+
sequenceTaskResult ((map f) opt)
387
+
#endif
388
+
324
389
/// <summary>
325
390
/// Converts a Option<Async<_>> to an Async<Option<_>>
0 commit comments