Skip to content

Commit a65ffeb

Browse files
committed
Add examples to documentation
1 parent 1bd3a22 commit a65ffeb

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

gitbook/option/sequenceAsync.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Option.sequenceAsync
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
Async<'a> option -> Async<'a option>
9+
```
10+
11+
Note that `sequence` is the same as `traverse id`. See also [Option.traverseAsyc](traverseAsync.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 a1 : Async<int option> =
21+
sequenceResult (Some (Async.singleton 42))
22+
// async { return Some 42 }
23+
24+
let a2 : Async<int option> =
25+
sequenceAsync None
26+
// async { return None }
27+
```

gitbook/option/traverseAsync.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Option.traverseResult
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
('a -> Async<'b>) -> 'a option -> Async<'b option>
9+
```
10+
11+
Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceAsync](sequenceAsync.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 -> Async<Customer>
32+
let getCustomerByEmail email : Async<Customer> = async {
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 `traverseAsync` function as below:
38+
39+
```fsharp
40+
Some "[email protected]" |> Option.traverseAsync getCustomerByEmail
41+
// async { return Some { Id = 1; Email = "[email protected]" } }
42+
43+
None |> Option.traverseResult tryParseInt
44+
// async { return None }
45+
```

0 commit comments

Comments
 (0)