Skip to content

Commit 37a24bd

Browse files
authored
Add new AsyncOption APIs and document all its other functions; minor fixes to documentation for Option module (#307)
* Add AsyncOption.orElse and orElseWith; add entries for Option.traverseAsync and sequenceAsync to SUMMARY.md; add documentation for the rest of the functions in module * Cleanup typos in type signatures in documentation
1 parent 7120202 commit 37a24bd

File tree

9 files changed

+444
-0
lines changed

9 files changed

+444
-0
lines changed

gitbook/SUMMARY.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
* [map](option/map.md)
4343
* [map2](option/map2.md)
4444
* [map3](option/map3.md)
45+
* [sequenceAsync](option/sequenceAsync.md)
4546
* [sequenceResult](option/sequenceResult.md)
4647
* [tee Functions](option/teeFunctions.md)
48+
* [traverseAsync](option/traverseAsync.md)
4749
* [traverseResult](option/traverseResult.md)
4850
* [zip](option/zip.md)
4951
* Lists
@@ -112,6 +114,15 @@
112114
* [ofTask](asyncResult/ofTask.md)
113115
* [ofTaskAction](asyncResult/ofTaskAction.md)
114116

117+
* AsyncOption
118+
* [apply](asyncOption/apply.md)
119+
* [bind](asyncOption/bind.md)
120+
* [Computation Expression](asyncOption/ce.md)
121+
* [either](asyncOption/either.md)
122+
* [map](asyncOption/map.md)
123+
* [orElse Functions](asyncOption/orElseFunctions.md)
124+
* [Other Functions](asyncOption/others.md)
125+
115126
* AsyncResultOption
116127
* [apply](asyncResultOption/apply.md)
117128
* [bind](asyncResultOption/bind.md)

gitbook/asyncOption/apply.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# AsyncOption.apply
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
Async<('a -> 'b) option> -> Async<'a option>
9+
-> Async<'b option>
10+
```
11+
12+
## Examples
13+
14+
Take the following function for example
15+
16+
```fsharp
17+
// string -> int
18+
let characterCount (s: string) = s.Length
19+
```
20+
21+
### Example 1
22+
23+
```fsharp
24+
let result =
25+
AsyncOption.some "foo" // Async<string option>
26+
|> AsyncOption.apply (AsyncOption.some characterCount) // Async<int option>
27+
28+
// async { Some 3 }
29+
```
30+
31+
### Example 2
32+
33+
```fsharp
34+
let result =
35+
Async.singleton None // Async<string option>
36+
|> AsyncOption.apply (AsyncOption.some characterCount) // Async<int option>
37+
38+
// async { None }
39+
```
40+
41+
### Example 3
42+
43+
```fsharp
44+
let result : Async<int option> =
45+
AsyncOption.some "foo" // Async<string option>
46+
|> AsyncOption.apply (Async.singleton None) // Async<int option>
47+
48+
// async { None }
49+
```

gitbook/asyncOption/bind.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# AsyncOption.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
('TInput -> Async<'TOutput option>) -> Async<'TInput option> -> Async<'TOutput option>
9+
```
10+
11+
## Examples
12+
13+
Take the following function for example
14+
15+
```fsharp
16+
type Account =
17+
{ EmailAddress : string
18+
Name : string }
19+
20+
// string -> Async<Account option>
21+
let lookupAccountByEmail email = async {
22+
let john = { EmailAddress = "[email protected]"; Name = "John Johnson" }
23+
let jeff = { EmailAddress = "[email protected]"; Name = "Jeff Jefferson" }
24+
let jack = { EmailAddress = "[email protected]"; Name = "Jack Jackson" }
25+
26+
// Just a map lookup, but imagine we look up an account in our database
27+
let accounts = Map.ofList [
28+
29+
30+
31+
]
32+
33+
return Map.tryFind email accounts
34+
}
35+
```
36+
37+
### Example 1
38+
39+
```fsharp
40+
let asyncOpt : Async<Account option> =
41+
AsyncOption.some "[email protected]" // Async<string option>
42+
|> AsyncOption.bind lookupAccountByEmail // Async<Account option>
43+
44+
// async { Some { EmailAddress = "[email protected]"; Name = "John Johnson" } }
45+
```
46+
47+
### Example 2
48+
49+
```fsharp
50+
let asyncOpt : Async<Account option> =
51+
AsyncOption.some "[email protected]" // Async<string option>
52+
|> AsyncOption.bind lookupAccountByEmail // Async<Account option>
53+
54+
// async { None }
55+
```
56+
57+
### Example 3
58+
59+
```fsharp
60+
let asyncOpt : Async<Account option> =
61+
Async.singleton None // Async<string option>
62+
|> AsyncOption.bind lookupAccountByEmail // Async<Account option>
63+
64+
// async { None }
65+
```

gitbook/asyncOption/either.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# AsyncOption.either
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
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.
8+
9+
```fsharp
10+
(onSome : 'T -> Async<'output>) -> (onNone : Async<'output>) -> (input : Async<'T option>) -> Async<'output>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
AsyncOption.either (fun x -> async { x * 2 }) (async { 0 }) (AsyncOption.some 5)
19+
20+
// async { 10 }
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
AsyncOption.either (fun x -> x * 2) (async { 0 }) None
27+
28+
// async { 0 }
29+
```
30+

gitbook/asyncOption/map.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# AsyncOption.map
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the value of an async option if it is `Some`. If the option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('TInput -> 'TOutput) -> Async<'TInput option> -> Async<'TOutput option>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
AsyncOption.map (fun x -> x + 1) (AsyncOption.some 1)
19+
20+
// async { Some 2 }
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
AsyncOption.map (fun x -> x + 1) (Async.singleton None)
27+
28+
// async { None }
29+
```
30+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# OrElse Functions
2+
3+
## AsyncOption.orElse
4+
5+
Namespace: `FsToolkit.ErrorHandling`
6+
7+
Returns the option if the option is Some, otherwise returns the given option
8+
9+
### Function Signature
10+
11+
```fsharp
12+
(ifNone : Async<'value option>) -> (input : Async<'value option>)
13+
-> Async<'value option>
14+
```
15+
16+
### Examples
17+
18+
#### Example 1
19+
20+
```fsharp
21+
let asyncOption : Async<int option> =
22+
AsyncOption.some 1
23+
|> AsyncOption.orElse (AsyncOption.some 2)
24+
25+
// async { Some 1 }
26+
```
27+
28+
#### Example 2
29+
30+
```fsharp
31+
let asyncOption : Async<int option> =
32+
AsyncOption.some 1
33+
|> AsyncOption.orElse (Async.singleton None)
34+
35+
// async { Some 1 }
36+
```
37+
38+
#### Example 3
39+
40+
```fsharp
41+
let asyncOption : Async<int option> =
42+
Async.singleton None
43+
|> AsyncOption.orElse (Some 2)
44+
45+
// async { Some 2 }
46+
```
47+
48+
#### Example 4
49+
50+
```fsharp
51+
let asyncOption : Async<int option> =
52+
Async.singleton None
53+
|> AsyncOption.orElse (Async.singleton None)
54+
55+
// async { None }
56+
```
57+
58+
## AsyncOption.orElseWith
59+
60+
Namespace: `FsToolkit.ErrorHandling`
61+
62+
Returns the option if the option is Some, otherwise evaluates the given function and returns the result.
63+
64+
### Function Signature
65+
66+
```fsharp
67+
(ifNoneFunc : unit -> Async<'value option>) -> (input : Async<'value option>)
68+
-> Async<'value option>
69+
```
70+
71+
### Examples
72+
73+
#### Example 1
74+
75+
```fsharp
76+
let asyncOption : Async<int option> =
77+
AsyncOption.some 1
78+
|> AsyncOption.orElseWith (fun () -> AsyncOption.some 2)
79+
80+
// async { Some 1 }
81+
```
82+
83+
#### Example 2
84+
85+
```fsharp
86+
let asyncOption : Async<int option> =
87+
AsyncOption.some 1
88+
|> AsyncOption.orElseWith (fun () -> None)
89+
90+
// async { Some 1 }
91+
```
92+
93+
#### Example 3
94+
95+
```fsharp
96+
let asyncOption : Async<int option> =
97+
Async.singleton None
98+
|> AsyncOption.orElseWith (fun () -> AsyncOption.some 2)
99+
100+
// async { Some 2 }
101+
```
102+
103+
#### Example 4
104+
105+
```fsharp
106+
let asyncOption : Async<int option> =
107+
Async.singleton None
108+
|> AsyncOption.orElseWith (fun () -> Async.singleton None)
109+
110+
// async { None }
111+
```

gitbook/asyncOption/others.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Other AsyncOption Functions
2+
3+
## defaultValue
4+
5+
Returns the contained value if Some, otherwise returns the provided value
6+
7+
### Function Signature
8+
9+
```fsharp
10+
'a -> Async<'a option> -> Async<'a>
11+
```
12+
13+
## defaultWith
14+
15+
Returns the contained value if Some, otherwise evaluates the given function and returns the result.
16+
17+
### Function Signature
18+
19+
```fsharp
20+
(unit -> 'a) -> Async<'a option> -> Async<'a>
21+
```
22+
23+
## some
24+
25+
Wraps the provided value in an Async<value option>
26+
27+
### Function Signature
28+
29+
```fsharp
30+
'a -> Async<'a option>
31+
```
32+
33+

0 commit comments

Comments
 (0)