-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Option.traverseAsync and Option.sequenceAsync (#298)
* Implement Option.traverseAsync and Option.sequenceAsync and add tests * Add examples to documentation * Fix typo in doc * One more doc fix * One more doc fix * One more doc fix
- Loading branch information
Showing
4 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## Option.sequenceAsync | ||
|
||
Namespace: `FsToolkit.ErrorHandling` | ||
|
||
Function Signature: | ||
|
||
```fsharp | ||
Async<'a> option -> Async<'a option> | ||
``` | ||
|
||
Note that `sequence` is the same as `traverse id`. See also [Option.traverseAsync](traverseAsync.md). | ||
|
||
See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
||
## Examples | ||
|
||
### Example 1 | ||
|
||
```fsharp | ||
let a1 : Async<int option> = | ||
Option.sequenceAsync (Some (Async.singleton 42)) | ||
// async { return Some 42 } | ||
let a2 : Async<int option> = | ||
Option.sequenceAsync None | ||
// async { return None } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
## Option.traverseAsync | ||
|
||
Namespace: `FsToolkit.ErrorHandling` | ||
|
||
Function Signature: | ||
|
||
```fsharp | ||
('a -> Async<'b>) -> 'a option -> Async<'b option> | ||
``` | ||
|
||
Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceAsync](sequenceAsync.md). | ||
|
||
See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
||
## Examples | ||
|
||
### Example 1 | ||
|
||
Let's assume we have a type `Customer`: | ||
|
||
```fsharp | ||
type Customer = { | ||
Id : int | ||
Email : string | ||
} | ||
``` | ||
|
||
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: | ||
|
||
```fsharp | ||
// string -> Async<Customer> | ||
let getCustomerByEmail email : Async<Customer> = async { | ||
return { Id = 1; Email = "[email protected]" } // return a constant for simplicity | ||
} | ||
``` | ||
|
||
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: | ||
|
||
```fsharp | ||
Some "[email protected]" |> Option.traverseAsync getCustomerByEmail | ||
// async { return Some { Id = 1; Email = "[email protected]" } } | ||
None |> Option.traverseAsync getCustomerByEmail | ||
// async { return None } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters