-
Notifications
You must be signed in to change notification settings - Fork 64
Add traversals/sequences for Task and TaskResult in the Option module
#325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheAngryByrd
merged 3 commits into
demystifyfp:master
from
tw0po1nt:add-option-traversals
May 1, 2025
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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.sequenceTask | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Function Signature: | ||
|
|
||
| ```fsharp | ||
| Task<'a> option -> Task<'a option> | ||
| ``` | ||
|
|
||
| Note that `sequence` is the same as `traverse id`. See also [Option.traverseTask](traverseTask.md). | ||
|
|
||
| See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let a1 : Task<int option> = | ||
| Option.sequenceTask (Some (Task.singleton 42)) | ||
| // async { return Some 42 } | ||
|
|
||
| let a2 : Task<int option> = | ||
| Option.sequenceTask None | ||
| // async { return None } | ||
| ``` |
This file contains hidden or 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,31 @@ | ||
| ## Option.sequenceTaskResult | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Function Signature: | ||
|
|
||
| ```fsharp | ||
| Task<Result<'a, 'e>> option -> Task<Result<'a option>, 'e> | ||
| ``` | ||
|
|
||
| Note that `sequence` is the same as `traverse id`. See also [Option.traverseTaskResult](traverseTaskResult.md). | ||
|
|
||
| See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let r1 : Task<Result<int option, string>> = | ||
| Some (task { return Ok 42 }) |> Option.sequenceTaskResult | ||
| // task { return Ok (Some 42) } | ||
|
|
||
| let r2 : Task<Result<int option, string>> = | ||
| Some (task { return Error "something went wrong" }) |> Option.sequenceTaskResult | ||
| // task { return Error "something went wrong" } | ||
|
|
||
| let r3 : Task<Result<int option, string>> = | ||
| None |> Option.sequenceTaskResult | ||
| // task { return Ok None } | ||
| ``` |
This file contains hidden or 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.traverseTask | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Function Signature: | ||
|
|
||
| ```fsharp | ||
| ('a -> Task<'b>) -> 'a option -> Task<'b option> | ||
| ``` | ||
|
|
||
| Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceTask](sequenceTask.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 -> Task<Customer> | ||
| let getCustomerByEmail email : Task<Customer> = task { | ||
| 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 `traverseTask` function as below: | ||
|
|
||
| ```fsharp | ||
| Some "[email protected]" |> Option.traverseTask getCustomerByEmail | ||
| // task { return Some { Id = 1; Email = "[email protected]" } } | ||
|
|
||
| None |> Option.traverseTask getCustomerByEmail | ||
| // task { return None } | ||
| ``` |
This file contains hidden or 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,44 @@ | ||
| ## Option.traverseTaskResult | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Function Signature: | ||
|
|
||
| ```fsharp | ||
| ('a -> Task<Result<'b,'c>>) -> 'a option -> Task<Result<'b option, 'c>> | ||
| ``` | ||
|
|
||
| Note that `traverse` is the same as `map >> sequence`. See also [Option.sequenceTaskResult](sequenceTaskResult.md). | ||
|
|
||
| See also Scott Wlaschin's [Understanding traverse and sequence](https://fsharpforfunandprofit.com/posts/elevated-world-4/). | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| Say we have a function to get a number from a database (asynchronously), and multiply our input by that number if it's found: | ||
|
|
||
| ```fsharp | ||
| let tryMultiplyWithDatabaseValue: float -> Task<Result<float, string>> = // ... | ||
| ``` | ||
|
|
||
| If we start with an optional value, then we could map this function using `Option.traverseTaskResult` as follows: | ||
|
|
||
| ```fsharp | ||
| let input = Some 1.234 | ||
|
|
||
| input // float option | ||
| |> Option.traverseTaskResult tryMultiplyWithDatabaseValue // Task<Result<float option, string>> | ||
| ``` | ||
|
|
||
| If we combine this with the [TaskResult computation expression](../taskResult/ce.md), we could directly `let!` the output: | ||
|
|
||
| ```fsharp | ||
| taskResult { | ||
| let input = Some 1.234 | ||
|
|
||
| let! output = // float option | ||
| input // float option | ||
| |> Option.traverseTaskResult tryMultiplyWithDatabaseValue // Task<Result<float option, string>> | ||
| } | ||
| ``` |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.