-
Notifications
You must be signed in to change notification settings - Fork 64
Add new AsyncOption APIs and document all its other functions; minor fixes to documentation for Option module
#307
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 2 commits into
demystifyfp:master
from
tw0po1nt:asyncoption-orelse-orelsewith
Feb 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 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,49 @@ | ||
| # AsyncOption.apply | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Function Signature: | ||
|
|
||
| ```fsharp | ||
| Async<('a -> 'b) option> -> Async<'a option> | ||
| -> Async<'b option> | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| Take the following function for example | ||
|
|
||
| ```fsharp | ||
| // string -> int | ||
| let characterCount (s: string) = s.Length | ||
| ``` | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let result = | ||
| AsyncOption.some "foo" // Async<string option> | ||
| |> AsyncOption.apply (AsyncOption.some characterCount) // Async<int option> | ||
|
|
||
| // async { Some 3 } | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| let result = | ||
| Async.singleton None // Async<string option> | ||
| |> AsyncOption.apply (AsyncOption.some characterCount) // Async<int option> | ||
|
|
||
| // async { None } | ||
| ``` | ||
|
|
||
| ### Example 3 | ||
|
|
||
| ```fsharp | ||
| let result : Async<int option> = | ||
| AsyncOption.some "foo" // Async<string option> | ||
| |> AsyncOption.apply (Async.singleton None) // Async<int option> | ||
|
|
||
| // async { 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,65 @@ | ||
| # AsyncOption.bind | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| ## Function Signature | ||
|
|
||
| ```fsharp | ||
| ('TInput -> Async<'TOutput option>) -> Async<'TInput option> -> Async<'TOutput option> | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| Take the following function for example | ||
|
|
||
| ```fsharp | ||
| type Account = | ||
| { EmailAddress : string | ||
| Name : string } | ||
|
|
||
| // string -> Async<Account option> | ||
| let lookupAccountByEmail email = async { | ||
| let john = { EmailAddress = "[email protected]"; Name = "John Johnson" } | ||
| let jeff = { EmailAddress = "[email protected]"; Name = "Jeff Jefferson" } | ||
| let jack = { EmailAddress = "[email protected]"; Name = "Jack Jackson" } | ||
|
|
||
| // Just a map lookup, but imagine we look up an account in our database | ||
| let accounts = Map.ofList [ | ||
| ("[email protected]", john) | ||
| ("[email protected]", jeff) | ||
| ("[email protected]", jack) | ||
| ] | ||
|
|
||
| return Map.tryFind email accounts | ||
| } | ||
| ``` | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| let asyncOpt : Async<Account option> = | ||
| AsyncOption.some "[email protected]" // Async<string option> | ||
| |> AsyncOption.bind lookupAccountByEmail // Async<Account option> | ||
|
|
||
| // async { Some { EmailAddress = "[email protected]"; Name = "John Johnson" } } | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| let asyncOpt : Async<Account option> = | ||
| AsyncOption.some "[email protected]" // Async<string option> | ||
| |> AsyncOption.bind lookupAccountByEmail // Async<Account option> | ||
|
|
||
| // async { None } | ||
| ``` | ||
|
|
||
| ### Example 3 | ||
|
|
||
| ```fsharp | ||
| let asyncOpt : Async<Account option> = | ||
| Async.singleton None // Async<string option> | ||
| |> AsyncOption.bind lookupAccountByEmail // Async<Account option> | ||
|
|
||
| // async { 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,30 @@ | ||
| # AsyncOption.either | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| ## Function Signature | ||
|
|
||
| Provide two functions to execute depending on the value of the option. If the option is `Some`, the first function will be executed. If the option is `None`, the second function will be executed. | ||
|
|
||
| ```fsharp | ||
| (onSome : 'T -> Async<'output>) -> (onNone : Async<'output>) -> (input : Async<'T option>) -> Async<'output> | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| AsyncOption.either (fun x -> async { x * 2 }) (async { 0 }) (AsyncOption.some 5) | ||
|
|
||
| // async { 10 } | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| AsyncOption.either (fun x -> x * 2) (async { 0 }) None | ||
|
|
||
| // async { 0 } | ||
| ``` | ||
|
|
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,30 @@ | ||
| # AsyncOption.map | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Apply a function to the value of an async option if it is `Some`. If the option is `None`, return `None`. | ||
|
|
||
| ## Function Signature | ||
|
|
||
| ```fsharp | ||
| ('TInput -> 'TOutput) -> AsyncOption<'TInput> -> AsyncOption<'TOutput> | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```fsharp | ||
| AsyncOption.map (fun x -> x + 1) (AsyncOption.some 1) | ||
|
|
||
| // async { Some 2 } | ||
| ``` | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```fsharp | ||
| AsyncOption.map (fun x -> x + 1) (Async.singleton None) | ||
|
|
||
| // async { 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,111 @@ | ||
| # OrElse Functions | ||
|
|
||
| ## AsyncOption.orElse | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Returns the option if the option is Some, otherwise returns the given option | ||
|
|
||
| ### Function Signature | ||
|
|
||
| ```fsharp | ||
| (ifNone : Option<'value>) -> (input : Option<'value>) | ||
| -> Option<'value> | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| #### Example 1 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| AsyncOption.some 1 | ||
| |> AsyncOption.orElse (AsyncOption.some 2) | ||
|
|
||
| // async { Some 1 } | ||
| ``` | ||
|
|
||
| #### Example 2 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| AsyncOption.some 1 | ||
| |> AsyncOption.orElse (Async.singleton None) | ||
|
|
||
| // async { Some 1 } | ||
| ``` | ||
|
|
||
| #### Example 3 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| Async.singleton None | ||
| |> AsyncOption.orElse (Some 2) | ||
|
|
||
| // async { Some 2 } | ||
| ``` | ||
|
|
||
| #### Example 4 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| Async.singleton None | ||
| |> AsyncOption.orElse (Async.singleton None) | ||
|
|
||
| // async { None } | ||
| ``` | ||
|
|
||
| ## AsyncOption.orElseWith | ||
|
|
||
| Namespace: `FsToolkit.ErrorHandling` | ||
|
|
||
| Returns the option if the option is Some, otherwise evaluates the given function and returns the result. | ||
|
|
||
| ### Function Signature | ||
|
|
||
| ```fsharp | ||
| (ifNoneFunc : unit -> AsyncOption<'value>) -> (input : AsyncOption<'value>) | ||
| -> AsyncOption<'value> | ||
| ``` | ||
|
|
||
| ### Examples | ||
|
|
||
| #### Example 1 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| AsyncOption.some 1 | ||
| |> AsyncOption.orElseWith (fun () -> AsyncOption.some 2) | ||
|
|
||
| // async { Some 1 } | ||
| ``` | ||
|
|
||
| #### Example 2 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| AsyncOption.some 1 | ||
| |> AsyncOption.orElseWith (fun () -> None) | ||
|
|
||
| // async { Some 1 } | ||
| ``` | ||
|
|
||
| #### Example 3 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| Async.singleton None | ||
| |> AsyncOption.orElseWith (fun () -> AsyncOption.some 2) | ||
|
|
||
| // async { Some 2 } | ||
| ``` | ||
|
|
||
| #### Example 4 | ||
|
|
||
| ```fsharp | ||
| let asyncOption : AsyncOption<int> = | ||
| Async.singleton None | ||
| |> AsyncOption.orElseWith (fun () -> Async.singleton None) | ||
|
|
||
| // async { 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,33 @@ | ||
| # Other AsyncOption Functions | ||
|
|
||
| ## defaultValue | ||
|
|
||
| Returns the contained value if Some, otherwise returns the provided value | ||
|
|
||
| ### Function Signature | ||
|
|
||
| ```fsharp | ||
| 'a -> Async<'a option> -> Async<'a> | ||
| ``` | ||
|
|
||
| ## defaultWith | ||
|
|
||
| Returns the contained value if Some, otherwise evaluates the given function and returns the result. | ||
|
|
||
| ### Function Signature | ||
|
|
||
| ```fsharp | ||
| (unit -> 'a) -> Async<'a option> -> Async<'a> | ||
| ``` | ||
|
|
||
| ## some | ||
|
|
||
| Wraps the provided value in an Async<value option> | ||
|
|
||
| ### Function Signature | ||
|
|
||
| ```fsharp | ||
| 'a -> Async<'a option> | ||
| ``` | ||
|
|
||
|
|
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.