Skip to content

Commit 3874432

Browse files
committed
Finish adding documentation for modules thru Task
1 parent d7e69da commit 3874432

File tree

15 files changed

+437
-18
lines changed

15 files changed

+437
-18
lines changed

gitbook/SUMMARY.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* [map](resultOption/map.md)
7474
* [map2](resultOption/map2.md)
7575
* [map3](resultOption/map3.md)
76-
* [mapError](pr.md)
76+
* [mapError](resultOption/mapError.md)
7777
* [Operators](resultOption/operators.md)
7878
* [zip](resultOption/zip.md)
7979
* [zipError](resultOption/zipError.md)
@@ -96,8 +96,8 @@
9696
* [mapError](asyncResult/mapError.md)
9797
* [Operators](asyncResult/operators.md)
9898
* [Other Functions](asyncResult/others.md)
99-
* [zip](pr.md)
100-
* [zipError](pr.md)
99+
* [zip](asyncResult/zip.md)
100+
* [zipError](asyncResult/zipError.md)
101101
* List
102102
* [traverseAsyncResultM](list/traverseAsyncResultM.md)
103103
* [sequenceAsyncResultM](list/sequenceAsyncResultM.md)
@@ -139,18 +139,17 @@
139139
* [ofResult](asyncResultOption/ofResult.md)
140140

141141
* Task
142-
* [apply](pr.md)
143-
* [bind](pr.md)
144-
* [bindV](pr.md)
145-
* [catch](pr.md)
146-
* [Computation Expression](pr.md)
147-
* [ignore](pr.md)
148-
* [map](pr.md)
149-
* [mapV](pr.md)
150-
* [map2](pr.md)
151-
* [map3](pr.md)
152-
* [ofUnit](pr.md)
153-
* [zip](pr.md)
142+
* [apply](task/apply.md)
143+
* [bind](task/bind.md)
144+
* [bindV](task/bindV.md)
145+
* [catch](task/catch.md)
146+
* [Computation Expression](task/ce.md)
147+
* [ignore](task/ignore.md)
148+
* [map](task/map.md)
149+
* [mapV](task/mapV.md)
150+
* [map2](task/map2.md)
151+
* [map3](task/map3.md)
152+
* [zip](task/zip.md)
154153
* Transforms
155154
* [ofUnit](task/ofUnit.md)
156155

gitbook/asyncResult/zip.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AsyncResult.zip
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
Async<Result<'leftOk, 'error>> -> Async<Result<'rightOk, 'error>> -> Async<Result<'leftOk * 'rightOk, 'error>>
9+
```
10+
11+
## Examples
12+
13+
### Example 1
14+
15+
```fsharp
16+
let result = AsyncResult.zip (AsyncResult.ok 1) (AsyncResult.ok 2)
17+
// async { Ok (1, 2) }
18+
```
19+
20+
### Example 2
21+
22+
```fsharp
23+
let result = AsyncResult.zip (AsyncResult.ok 1) (AsyncResult.error "Bad")
24+
// async { Error "Bad" }
25+
```
26+
27+
### Example 3
28+
29+
```fsharp
30+
let result = AsyncResult.zip (AsyncResult.error "Bad1") (AsyncResult.error "Bad2")
31+
// async { Error "Bad1" }
32+
```

gitbook/asyncResult/zipError.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AsyncResult.zipError
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
Async<Result<'ok, 'leftError>> -> Async<Result<'ok, 'rightError>> -> Async<Result<'ok, 'leftError * 'rightError>>
9+
```
10+
11+
## Examples
12+
13+
### Example 1
14+
15+
```fsharp
16+
let result = AsyncResult.zipError (AsyncResult.ok 1) (AsyncResult.ok 2)
17+
// async { Ok 1 }
18+
```
19+
20+
### Example 2
21+
22+
```fsharp
23+
let result = AsyncResult.zipError (AsyncResult.ok 1) (AsyncResult.error "Bad")
24+
// async { Ok 1 }
25+
```
26+
27+
### Example 3
28+
29+
```fsharp
30+
let result = AsyncResult.zipError (AsyncResult.error "Bad1") (AsyncResult.error "Bad2")
31+
// async { Error("Bad1", "Bad2") }
32+
```

gitbook/result/zipError.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ Result<'ok, 'leftError> -> Result<'ok, 'rightError> -> Result<'ok, 'leftError *
1313
### Example 1
1414

1515
```fsharp
16-
let result = Result.zip (Ok 1) (Ok 2)
16+
let result = Result.zipError (Ok 1) (Ok 2)
1717
// Ok 1
1818
```
1919

2020
### Example 2
2121

2222
```fsharp
23-
let result = Result.zip (Ok 1) (Error "Bad")
23+
let result = Result.zipError (Ok 1) (Error "Bad")
2424
// Ok 1
2525
```
2626

2727
### Example 3
2828

2929
```fsharp
30-
let result = Result.zip (Error "Bad1") (Error "Bad2")
30+
let result = Result.zipError (Error "Bad1") (Error "Bad2")
3131
// Error("Bad1", "Bad2")
3232
```

gitbook/resultOption/mapError.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## ResultOption.mapError
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
('errorInput -> 'errorOutput) -> Result<'ok option, 'errorInput> -> Result<'ok option, 'errorOutput>
9+
```
10+
11+
## Examples
12+
13+
### Example 1
14+
15+
From the [ResultOption.map](../resultOption/map.md#example-3) example, if we want to map the error part alone, we can do it as below:
16+
17+
```fsharp
18+
// string -> int
19+
let getErrorCode (message: string) =
20+
match message with
21+
| "bad things happened" -> 1
22+
| _ -> 0
23+
24+
let result : Result<string option, int> =
25+
Error "bad things happened" // Result<string option, string>
26+
|> ResultOption.mapError getErrorCode // Result<string option, int>
27+
28+
// Error 1
29+
```
30+

gitbook/task/apply.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Task.apply
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
Task<('a -> 'b)> -> Task<'a> -> Task<'b>
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+
Task.singleton "foo" // Task<string>
25+
|> Task.apply (Task.singleton characterCount) // Task<int>
26+
27+
// task { 3 }
28+
```

gitbook/task/bind.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Task.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Function Signature:
6+
7+
```fsharp
8+
('a -> Task<'b>>) -> Task<'a> -> Task<'b>
9+
```
10+
11+
## Examples
12+
13+
Note: Many use-cases requiring `bind` operations can also be solved using [the `task` computation expression](../task/ce.md).
14+
15+
### Example 1
16+
17+
Continuing from the [Task.map2 example](../task/map2.md#example-1) and given the function
18+
19+
```fsharp
20+
let notifyFollowers : NotifyNewPostRequest -> Task<unit>
21+
```
22+
23+
we can notify all followers using `Task.bind` as below:
24+
25+
```fsharp
26+
newPostRequestResult |> Task.bind notifyFollowers
27+
```
28+

gitbook/task/bindV.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Task.bindV
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Like [Task.bind](../task/bind.md), but taking a `ValueTask<'a>` as input
6+
7+
Function Signature:
8+
9+
```fsharp
10+
('a -> Task<'b>>) -> ValueTask<'a> -> Task<'b>
11+
```
12+
13+
## Examples
14+
15+
Note: Many use-cases requiring `bind` operations can also be solved using [the `task` computation expression](../task/ce.md).
16+
17+
### Example 1
18+
19+
Continuing from the [Task.map2 example](../task/map2.md#example-1 and given the function
20+
21+
```fsharp
22+
let notifyFollowers : NotifyNewPostRequest -> Task<unit>
23+
```
24+
25+
and assuming `newPostRequestResult` has type `ValueTask<NotifyNewPostRequest>`
26+
27+
we can notify all followers using `Task.bindV` as below:
28+
29+
```fsharp
30+
newPostRequestResult |> Task.bindV notifyFollowers
31+
```
32+

gitbook/task/catch.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Task.catch
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Creates a `Task` that attempts to execute the provided task, returning `Choice1Of2` with the result if the task completes without exceptions, or `Choice2Of2` with the exception if an exception is thrown.
6+
7+
Function Signature:
8+
9+
```fsharp
10+
Task<'a> -> Task<Choice<'a, exn>>
11+
```
12+
13+
## Examples
14+
15+
Given the function:
16+
17+
```fsharp
18+
let taskThrow () =
19+
task {
20+
failwith "something bad happened"
21+
return Error ""
22+
}
23+
```
24+
25+
### Example 1
26+
27+
```fsharp
28+
let result = Task.catch (Task.singleton 42)
29+
// task { Choice1Of2(42) }
30+
```
31+
32+
### Example 2
33+
34+
Given the function
35+
36+
```fsharp
37+
let result = Task.catch (taskThrow ())
38+
// task { Choice2Of2(exn("something bad happened")) }
39+
```
40+

gitbook/task/ce.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Task 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+
parseInt : string -> int
13+
findPersonById : int -> Task<Person>
14+
updatePerson : Person -> Task<unit>
15+
```
16+
17+
```fsharp
18+
// Task<unit>
19+
let addResult = task {
20+
let personId = parseInt "3001"
21+
let age = parseInt "35"
22+
let! person = findPersonById personId
23+
let person = { person with Age = age }
24+
do! updatePerson person
25+
}
26+
```

0 commit comments

Comments
 (0)