Skip to content

Commit 75f2214

Browse files
authored
Add CancellableTaskOption module and CE + tests and documentation (#328)
1 parent 66a2757 commit 75f2214

File tree

14 files changed

+2897
-2
lines changed

14 files changed

+2897
-2
lines changed

gitbook/SUMMARY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@
254254
* [Computation Expression](pr.md)
255255

256256
* FsToolkit.ErrorHandling.IcedTasks
257+
* CancellableTaskOption
258+
* [apply](cancellableTaskOption/apply.md)
259+
* [bind](cancellableTaskOption/bind.md)
260+
* [Computation Expression](cancellableTaskOption/ce.md)
261+
* [either](cancellableTaskOption/either.md)
262+
* [map](cancellableTaskOption/map.md)
263+
* [Other Functions](cancellableTaskOption/others.md)
264+
* [zip](cancellableTaskOption/zip.md)
265+
257266
* [CancellableTaskResult](cancellableTaskResult/index.md)
258267
* [apply](cancellableTaskResult/apply.md)
259268
* [bind](cancellableTaskResult/bind.md)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# CancellableTaskOption.apply
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
CancellableTask<('a -> 'b) option> -> CancellableTask<'a option> -> CancellableTask<'b option>
9+
```
10+
11+
## Examples
12+
13+
Take the following function for example
14+
15+
```fsharp
16+
// string -> int
17+
let characterCount (s: string) = s.Length
18+
```
19+
20+
### Example 1
21+
22+
```fsharp
23+
let result =
24+
CancellableTaskOption.some "foo" // CancellableTask<string option>
25+
|> CancellableTaskOption.apply (CancellableTaskOption.some characterCount) // CancellableTask<int option>
26+
27+
// cancellableTask { Some 3 }
28+
```
29+
30+
### Example 2
31+
32+
```fsharp
33+
let result =
34+
CancellableTask.singleton None // CancellableTask<string option>
35+
|> CancellableTaskOption.apply (CancellableTaskOption.some characterCount) // CancellableTask<int option>
36+
37+
// cancellableTask { None }
38+
```
39+
40+
### Example 3
41+
42+
```fsharp
43+
let result : CancellableTask<int option> =
44+
CancellableTaskOption.some "foo" // CancellableTask<string option>
45+
|> CancellableTaskOption.apply (CancellableTask.singleton None) // CancellableTask<int option>
46+
47+
// cancellableTask { None }
48+
```

gitbook/cancellableTaskOption/bind.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# CancellableTaskOption.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
('input -> CancellableTask<'output option>) -> CancellableTask<'input option> -> CancellableTask<'output 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 -> CancellableTask<Account option>
21+
let lookupAccountByEmail email = cancellableTask {
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 taskOpt : CancellableTask<Account option> =
41+
CancellableTaskOption.some "[email protected]" // CancellableTask<string option>
42+
|> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option>
43+
44+
// cancellableTask { Some { EmailAddress = "[email protected]"; Name = "John Johnson" } }
45+
```
46+
47+
### Example 2
48+
49+
```fsharp
50+
let taskOpt : CancellableTask<Account option> =
51+
CancellableTaskOption.some "[email protected]" // CancellableTask<string option>
52+
|> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option>
53+
54+
// cancellableTask { None }
55+
```
56+
57+
### Example 3
58+
59+
```fsharp
60+
let taskOpt : CancellableTask<Account option> =
61+
CancellableTask.singleton None // CancellableTask<string option>
62+
|> CancellableTaskOption.bind lookupAccountByEmail // CancellableTask<Account option>
63+
64+
// cancellableTask { None }
65+
```

gitbook/cancellableTaskOption/ce.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## CancellableTaskOption Computation Expression
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Examples:
6+
7+
### Example 1
8+
9+
Given a personId and an age, find a person and update their age.
10+
11+
```fsharp
12+
tryParseInt : string -> Option<int>
13+
tryFindPersonById : int -> CancellableTask<Person option>
14+
updatePerson : Person -> CancellableTask<unit>
15+
```
16+
17+
```fsharp
18+
// CancellableTask<unit option>
19+
let addResult = cancellableTaskOption {
20+
let! personId = tryParseInt "3001"
21+
let! age = tryParseInt "35"
22+
let! person = tryFindPersonById personId
23+
let person = { person with Age = age }
24+
do! updatePerson person
25+
}
26+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CancellableTaskOption.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 -> CancellableTask<'output>)
11+
-> (onNone : CancellableTask<'output>)
12+
-> (input : CancellableTask<'T option>)
13+
-> CancellableTask<'output>
14+
```
15+
16+
## Examples
17+
18+
### Example 1
19+
20+
```fsharp
21+
CancellableTaskOption.either (fun x -> cancellableTask { x * 2 }) (cancellableTask { 0 }) (CancellableTaskOption.some 5)
22+
23+
// cancellableTask { 10 }
24+
```
25+
26+
### Example 2
27+
28+
```fsharp
29+
CancellableTaskOption.either (fun x -> x * 2) (cancellableTask { 0 }) None
30+
31+
// cancellableTask { 0 }
32+
```
33+

gitbook/cancellableTaskOption/map.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# CancellableTaskOption.map
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the value of a cancellable task option if it is `Some`. If the option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('input -> 'output) -> CancellableTask<'input option> -> CancellableTask<'output option>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
CancellableTaskOption.map (fun x -> x + 1) (CancellableTaskOption.some 1)
19+
20+
// cancellableTask { Some 2 }
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
CancellableTaskOption.map (fun x -> x + 1) (CancellableTask.singleton None)
27+
28+
// cancellableTask { None }
29+
```
30+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Other CancellableTaskOption 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 -> CancellableTask<'a option> -> CancellableTask<'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) -> CancellableTask<'a option> -> CancellableTask<'a>
21+
```
22+
23+
## some
24+
25+
Wraps the provided value in an CancellableTask<'a option>
26+
27+
### Function Signature
28+
29+
```fsharp
30+
'a -> CancellableTask<'a option>
31+
```
32+
33+

gitbook/cancellableTaskOption/zip.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CancellableTaskOption.zip
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Takes two options and returns a tuple of the pair or None if either are None
6+
7+
## Function Signature
8+
9+
```fsharp
10+
CancellableTask<'left option> -> CancellableTask<'right option> -> CancellableTask<('left * 'right) option>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
let left = CancellableTaskOption.some 123
19+
let right = CancellableTaskOption.some "abc"
20+
21+
CancellableTaskOption.zip left right
22+
// cancellableTask { Some (123, "abc") }
23+
```
24+
25+
### Example 2
26+
27+
```fsharp
28+
let left = CancellableTaskOption.some 123
29+
let right = CancellableTaskOption.singleton None
30+
31+
CancellableTaskOption.zip left right
32+
// cancellableTask { None }
33+
```

gitbook/taskOption/ce.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ updatePerson : Person -> Task<unit>
1919
let addResult = taskOption {
2020
let! personId = tryParseInt "3001"
2121
let! age = tryParseInt "35"
22-
let! person = tryFindPersonById personId "US-OH"
22+
let! person = tryFindPersonById personId
2323
let person = { person with Age = age }
2424
do! updatePerson person
2525
}
26-
```
26+
```

0 commit comments

Comments
 (0)