Skip to content

Commit d10f132

Browse files
Add cancellableValueTaskOption and cancellableValueTaskResult CEs for IcedTasks (#349)
* Initial plan * Add cancellableValueTaskOption and cancellableValueTaskResult CEs for IcedTasks Co-authored-by: TheAngryByrd <1490044+TheAngryByrd@users.noreply.github.com> * Fix some function, add doc comments, and add gitbook documentation Co-authored-by: TheAngryByrd <1490044+TheAngryByrd@users.noreply.github.com> * Avoid state machine creation in CancellableValueTaskOption.some Co-authored-by: TheAngryByrd <1490044+TheAngryByrd@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TheAngryByrd <1490044+TheAngryByrd@users.noreply.github.com>
1 parent d91aaa4 commit d10f132

21 files changed

Lines changed: 3299 additions & 9 deletions

gitbook/SUMMARY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@
310310
* [ofChoice](cancellableTaskValidation/ofChoice.md)
311311
* [ofResult](cancellableTaskValidation/ofResult.md)
312312

313+
* CancellableValueTaskOption
314+
* [apply](cancellableValueTaskOption/apply.md)
315+
* [bind](cancellableValueTaskOption/bind.md)
316+
* [Computation Expression](cancellableValueTaskOption/ce.md)
317+
* [either](cancellableValueTaskOption/either.md)
318+
* [map](cancellableValueTaskOption/map.md)
319+
* [Other Functions](cancellableValueTaskOption/others.md)
320+
* [zip](cancellableValueTaskOption/zip.md)
321+
322+
* [CancellableValueTaskResult](cancellableValueTaskResult/index.md)
323+
* [apply](cancellableValueTaskResult/apply.md)
324+
* [bind](cancellableValueTaskResult/bind.md)
325+
* [Computation Expression](cancellableValueTaskResult/ce.md)
326+
* [map](cancellableValueTaskResult/map.md)
327+
* [zip](cancellableValueTaskResult/zip.md)
328+
313329
* FsToolkit.ErrorHandling.JobResult
314330
* Job
315331
* [apply](pr.md)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# CancellableValueTaskOption.apply
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
CancellableValueTask<('a -> 'b) option> -> CancellableValueTask<'a option> -> CancellableValueTask<'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+
CancellableValueTaskOption.some "foo" // CancellableValueTask<string option>
25+
|> CancellableValueTaskOption.apply (CancellableValueTaskOption.some characterCount) // CancellableValueTask<int option>
26+
27+
// cancellableValueTask { Some 3 }
28+
```
29+
30+
### Example 2
31+
32+
```fsharp
33+
let result =
34+
CancellableValueTask.singleton None // CancellableValueTask<string option>
35+
|> CancellableValueTaskOption.apply (CancellableValueTaskOption.some characterCount) // CancellableValueTask<int option>
36+
37+
// cancellableValueTask { None }
38+
```
39+
40+
### Example 3
41+
42+
```fsharp
43+
let result : CancellableValueTask<int option> =
44+
CancellableValueTaskOption.some "foo" // CancellableValueTask<string option>
45+
|> CancellableValueTaskOption.apply (CancellableValueTask.singleton None) // CancellableValueTask<int option>
46+
47+
// cancellableValueTask { None }
48+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# CancellableValueTaskOption.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
('input -> CancellableValueTask<'output option>) -> CancellableValueTask<'input option> -> CancellableValueTask<'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 -> CancellableValueTask<Account option>
21+
let lookupAccountByEmail email = cancellableValueTask {
22+
let john = { EmailAddress = "john@test.com"; Name = "John Johnson" }
23+
let jeff = { EmailAddress = "jeff@test.com"; Name = "Jeff Jefferson" }
24+
let jack = { EmailAddress = "jack@test.com"; Name = "Jack Jackson" }
25+
26+
let accounts = Map.ofList [
27+
("john@test.com", john)
28+
("jeff@test.com", jeff)
29+
("jack@test.com", jack)
30+
]
31+
32+
return Map.tryFind email accounts
33+
}
34+
```
35+
36+
### Example 1
37+
38+
```fsharp
39+
let taskOpt : CancellableValueTask<Account option> =
40+
CancellableValueTaskOption.some "john@test.com" // CancellableValueTask<string option>
41+
|> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option>
42+
43+
// cancellableValueTask { Some { EmailAddress = "john@test.com"; Name = "John Johnson" } }
44+
```
45+
46+
### Example 2
47+
48+
```fsharp
49+
let taskOpt : CancellableValueTask<Account option> =
50+
CancellableValueTaskOption.some "jerry@test.com" // CancellableValueTask<string option>
51+
|> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option>
52+
53+
// cancellableValueTask { None }
54+
```
55+
56+
### Example 3
57+
58+
```fsharp
59+
let taskOpt : CancellableValueTask<Account option> =
60+
CancellableValueTask.singleton None // CancellableValueTask<string option>
61+
|> CancellableValueTaskOption.bind lookupAccountByEmail // CancellableValueTask<Account option>
62+
63+
// cancellableValueTask { None }
64+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## CancellableValueTaskOption 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 -> int option
13+
tryFindPersonById : int -> CancellableValueTask<Person option>
14+
updatePerson : Person -> CancellableValueTask<unit>
15+
```
16+
17+
```fsharp
18+
// CancellableValueTask<unit option>
19+
let addResult = cancellableValueTaskOption {
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# CancellableValueTaskOption.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 -> CancellableValueTask<'output>)
11+
-> (onNone : unit -> CancellableValueTask<'output>)
12+
-> (input : CancellableValueTask<'T option>)
13+
-> CancellableValueTask<'output>
14+
```
15+
16+
## Examples
17+
18+
### Example 1
19+
20+
```fsharp
21+
CancellableValueTaskOption.either (fun x -> cancellableValueTask { return x * 2 }) (fun () -> cancellableValueTask { return 0 }) (CancellableValueTaskOption.some 5)
22+
23+
// cancellableValueTask { 10 }
24+
```
25+
26+
### Example 2
27+
28+
```fsharp
29+
CancellableValueTaskOption.either (fun x -> cancellableValueTask { return x * 2 }) (fun () -> cancellableValueTask { return 0 }) (CancellableValueTask.singleton None)
30+
31+
// cancellableValueTask { 0 }
32+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CancellableValueTaskOption.map
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the value of a cancellable value task option if it is `Some`. If the option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('input -> 'output) -> CancellableValueTask<'input option> -> CancellableValueTask<'output option>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
CancellableValueTaskOption.map (fun x -> x + 1) (CancellableValueTaskOption.some 1)
19+
20+
// cancellableValueTask { Some 2 }
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
CancellableValueTaskOption.map (fun x -> x + 1) (CancellableValueTask.singleton None)
27+
28+
// cancellableValueTask { None }
29+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Other CancellableValueTaskOption 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 -> CancellableValueTask<'a option> -> CancellableValueTask<'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) -> CancellableValueTask<'a option> -> CancellableValueTask<'a>
21+
```
22+
23+
## some
24+
25+
Wraps the provided value in a `CancellableValueTask<'a option>`.
26+
27+
### Function Signature
28+
29+
```fsharp
30+
'a -> CancellableValueTask<'a option>
31+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# CancellableValueTaskOption.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+
CancellableValueTask<'left option> -> CancellableValueTask<'right option> -> CancellableValueTask<('left * 'right) option>
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
let left = CancellableValueTaskOption.some 123
19+
let right = CancellableValueTaskOption.some "abc"
20+
21+
CancellableValueTaskOption.zip left right
22+
// cancellableValueTask { Some (123, "abc") }
23+
```
24+
25+
### Example 2
26+
27+
```fsharp
28+
let left = CancellableValueTaskOption.some 123
29+
let right = CancellableValueTask.singleton None
30+
31+
CancellableValueTaskOption.zip left right
32+
// cancellableValueTask { None }
33+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# CancellableValueTaskResult.apply
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
CancellableValueTask<Result<('a -> 'b), 'c>> -> CancellableValueTask<Result<'a, 'c>> -> CancellableValueTask<Result<'b, 'c>>
9+
```
10+
11+
## Examples
12+
13+
### Example 1
14+
15+
```fsharp
16+
let result =
17+
CancellableValueTaskResult.singleton "foo"
18+
|> CancellableValueTaskResult.apply (CancellableValueTaskResult.singleton String.length)
19+
20+
// cancellableValueTask { Ok 3 }
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
let err : CancellableValueTask<Result<int, string>> = cancellableValueTask { return Error "some error" }
27+
let result =
28+
err
29+
|> CancellableValueTaskResult.apply (CancellableValueTaskResult.singleton String.length)
30+
31+
// cancellableValueTask { Error "some error" }
32+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# CancellableValueTaskResult.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
('a -> CancellableValueTask<Result<'b, 'c>>)
9+
-> CancellableValueTask<Result<'a, 'c>>
10+
-> CancellableValueTask<Result<'b, 'c>>
11+
```
12+
13+
## Examples
14+
15+
Note: Many use-cases requiring `bind` operations can also be solved using [the `cancellableValueTaskResult` computation expression](../cancellableValueTaskResult/ce.md).
16+
17+
### Example 1
18+
19+
Continuing from the CancellableValueTaskResult.map2 example and given the function
20+
21+
```fsharp
22+
let notifyFollowers : NotifyNewPostRequest -> CancellableValueTask<Result<unit,exn>>
23+
```
24+
25+
We can notify all followers using `CancellableValueTaskResult.bind` as below:
26+
27+
```fsharp
28+
newPostRequestResult |> CancellableValueTaskResult.bind notifyFollowers
29+
```

0 commit comments

Comments
 (0)