Skip to content

Commit d414fb0

Browse files
committed
add gitbook pages
1 parent b7fdd99 commit d414fb0

File tree

3 files changed

+85
-6
lines changed

3 files changed

+85
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Option.sequenceAsyncResult
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
Async<Result<'a, 'e>> option -> Async<Result<'a option>, 'e>
9+
```
10+
11+
Note that `sequence` is the same as `traverse id`. See also [Option.traverseAsyncResult](traverseAsyncResult.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+
```fsharp
20+
let r1 : Async<Result<int option, string>> =
21+
Some (async { return Ok 42 }) |> Option.sequenceAsyncResult
22+
// async { return Ok (Some 42) }
23+
24+
let r2 : Async<Result<int option, string>> =
25+
Some (async { return Error "something went wrong" }) |> Option.sequenceAsyncResult
26+
// async { return Error "something went wrong" }
27+
28+
let r3 : Async<Result<int option, string>> =
29+
None |> Option.sequenceAsyncResult
30+
// async { return Ok None }
31+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## Option.traverseAsyncResult
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
('a -> Async<Result<'b,'c>>) -> 'a option -> Async<Result<'b option, 'c>>
9+
```
10+
11+
Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceAsyncResult](sequenceAsyncResult.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+
Say we have a function to get a number from a database (asynchronously), and multiply our input by that number if it's found:
20+
21+
```fsharp
22+
let tryMultiplyWithDatabaseValue: float -> Async<Result<float, string>> = // ...
23+
```
24+
25+
If we start with an optional vaue, then we could map this funciton using `Option.traverseAsyncResult` as follows:
26+
27+
```fsharp
28+
let input = Some 1.234
29+
30+
input // float option
31+
|> Option.traverseAsyncResult tryMultiplyWithDatabaseValue // Async<Result<float option, string>>
32+
```
33+
34+
If we combine this with the [AsyncResult computation expression](../asyncResult/ce.md), we could directly `let!` the output:
35+
36+
```fsharp
37+
asyncResult {
38+
let input = Some 1.234
39+
40+
let! output = // float option
41+
input // float option
42+
|> Option.traverseAsyncResult tryMultiplyWithDatabaseValue // Async<Result<float option, string>>
43+
}
44+
```

src/FsToolkit.ErrorHandling/Option.fs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,13 @@ module Option =
350350
sequenceAsync ((map f) opt)
351351

352352
/// <summary>
353-
/// Converts a Option<Async<Result<'ok,'error>>> to an Async<Result<Option<'ok>,'error>>
353+
/// Converts a Async<Result<'ok,'error>> option to an Async<Result<'ok option,'error>>
354+
///
355+
/// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/sequenceasyncresult</href>
354356
/// </summary>
355357
let inline sequenceAsyncResult
356-
(optAsyncResult: Option<Async<Result<'T, 'E>>>)
357-
: Async<Result<Option<'T>, 'E>> =
358+
(optAsyncResult: Async<Result<'T, 'E>> option)
359+
: Async<Result<'T option, 'E>> =
358360
async {
359361
match optAsyncResult with
360362
| Some asncRes ->
@@ -367,15 +369,17 @@ module Option =
367369
}
368370

369371
/// <summary>
370-
/// Maps an AsyncResult function over an Option, returning an AsyncResult Option.
372+
/// Maps an AsyncResult function over an option, returning an AsyncResult option.
373+
///
374+
/// /// Documentation is found here: <href>https://demystifyfp.gitbook.io/fstoolkit-errorhandling/fstoolkit.errorhandling/option/traverseasyncresult</href>
371375
/// </summary>
372376
/// <param name="f">The function to map over the Option.</param>
373377
/// <param name="opt">The Option to map over.</param>
374378
/// <returns>An AsyncResult Option with the mapped value.</returns>
375379
let inline traverseAsyncResult
376380
([<InlineIfLambda>] f: 'T -> Async<Result<'U, 'E>>)
377-
(opt: Option<'T>)
378-
: Async<Result<Option<'U>, 'E>> =
381+
(opt: 'T option)
382+
: Async<Result<'U option, 'E>> =
379383
sequenceAsyncResult ((map f) opt)
380384

381385
/// <summary>

0 commit comments

Comments
 (0)