Skip to content

Commit 86b465a

Browse files
1eyewonderTheAngryByrd
authored andcommitted
Updated documentation and added unit tests
1 parent 520def9 commit 86b465a

File tree

12 files changed

+239
-22
lines changed

12 files changed

+239
-22
lines changed

gitbook/bindMappings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Each CE can bind to itself so we don't list that here to reduce clutter
3030
| JobResultOption | |
3131
| Option | ValueOption, nullable object |
3232
| Result | Choice |
33-
| ResultOption | |
33+
| ResultOption | Result, Choice, Option |
3434
| Task | |
3535
| TaskOption | ValueTaskOption, AsyncOption, PlyOption, Async, Task, ValueTask, Option |
3636
| TaskResult | AsyncResult, PlyResult, TaskResult, ValueTaskResult, Result, Choice, Ply, Async, Task, ValueTask |

gitbook/resultOption/apply.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.apply
1+
# ResultOption.apply
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -10,3 +10,40 @@ Result<('a -> 'b) option, 'c> -> Result<'a option, 'c>
1010
```
1111

1212
## 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+
Ok(Some "foo") // Result<string option, 'error>
26+
|> ResultOption.apply (Ok(Some characterCount)) // Result<int option, 'error>
27+
28+
// Ok (Some 3)
29+
```
30+
31+
### Example 2
32+
33+
```fsharp
34+
let result =
35+
Ok None // Result<string option, 'error>
36+
|> ResultOption.apply (Ok(Some characterCount)) // Result<int option, 'error>
37+
38+
// Ok None
39+
```
40+
41+
### Example 3
42+
43+
```fsharp
44+
let result =
45+
Error "bad things happened" // Result<string option, string>
46+
|> ResultOption.apply (Ok(Some characterCount)) // Result<int option, string>
47+
48+
// Error "bad things happened"
49+
```

gitbook/resultOption/bind.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.bind
1+
# ResultOption.bind
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -9,4 +9,54 @@ Function Signature:
99
-> Result<'b option, 'c>
1010
```
1111

12-
## Examples
12+
## Examples
13+
14+
Take the following function for example
15+
16+
```fsharp
17+
// string -> Result<int option, string>
18+
let tryParseInt (s: string) =
19+
match Int32.TryParse(s) with
20+
| true, i -> Ok(Some i)
21+
| false, _ -> Error "Could not parse string as int"
22+
```
23+
24+
### Example 1
25+
26+
```fsharp
27+
let result =
28+
Ok(Some "123") // Result<string option, 'error>
29+
|> ResultOption.bind tryParseInt // Result<int option, 'error>
30+
31+
// Ok (Some 123)
32+
```
33+
34+
### Example 2
35+
36+
```fsharp
37+
let result =
38+
Ok None // Result<string option, 'error>
39+
|> ResultOption.bind tryParseInt // Result<int option, 'error>
40+
41+
// Ok None
42+
```
43+
44+
### Example 3
45+
46+
```fsharp
47+
let result =
48+
Ok(Some "bad things happened") // Result<string option, string>
49+
|> ResultOption.bind tryParseInt // Result<int option, string>
50+
51+
// Error "Could not parse string as int"
52+
```
53+
54+
### Example 4
55+
56+
```fsharp
57+
let result =
58+
Error "bad things happened" // Result<string option, string>
59+
|> ResultOption.bind tryParseInt // Result<int option, string>
60+
61+
// Error "bad things happened"
62+
```

gitbook/resultOption/ce.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Result Computation Expression
1+
# Result Computation Expression
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -15,10 +15,25 @@ let addResult : Result<int option, string> = resultOption {
1515
let! z = Ok (Some 2)
1616
return add x y z
1717
}
18+
// Ok (Some 42)
1819
```
1920

2021
### Example 2
2122

23+
```fsharp
24+
let add x y z = x + y + z
25+
26+
let addResult : Result<int option, string> = resultOption {
27+
let! x = Ok (Some 30)
28+
and! y = Error "Oops 1"
29+
and! z = Error "Oops 2"
30+
return add x y z
31+
}
32+
// Error "Oops 1"
33+
```
34+
35+
### Example 3
36+
2237
The [ResultOption.map2 example](../resultOption/map2.md#example-2) can be written using the `resultOption` computation expression as below
2338

2439
```fsharp
@@ -41,4 +56,4 @@ let toCreatePostRequest (dto : CreatePostRequestDto) =
4156
4257
// Result<CreatePostRequest, string>
4358
Result.map2 createPostRequest2 tweetR locationR
44-
```
59+
```

gitbook/resultOption/ignore.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.ignore
1+
# ResultOption.ignore
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -24,4 +24,4 @@ We can call this with the `do!` syntax inside a computation expression using `Re
2424
let deletePost = resultOption {
2525
do! deletePostIfExists deletePostRequest |> ResultOption.ignore
2626
}
27-
```
27+
```

gitbook/resultOption/map.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.map
1+
# ResultOption.map
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -10,19 +10,42 @@ Function Signature:
1010

1111
`ResultOption.map` is the same as `Result.map Option.map`.
1212

13-
### Example 1
13+
## Examples
14+
15+
Take the following functions for example
16+
17+
```fsharp
18+
// string -> int
19+
let remainingCharacters (string: prompt) =
20+
280 - prompt.Length
21+
```
1422

15-
Given the following functions:
23+
### Example 1
1624

1725
```fsharp
18-
getTweet : PostId -> Result<Tweet option, _>
19-
remainingCharacters : Tweet -> int
26+
let result =
27+
Ok(Some "foo") // Result<string option, 'error>
28+
|> ResultOption.map remainingCharacters // Result<int option, 'error>
29+
30+
// Ok (Some 277)
2031
```
2132

22-
You can get the number of remaining characters as below:
33+
### Example 2
2334

2435
```fsharp
25-
getTweetByPostId somePostId
26-
|> ResultOption.map remainingCharacters
36+
let result =
37+
Ok None // Result<string option, 'error>
38+
|> ResultOption.map remainingCharacters // Result<int option, 'error>
39+
40+
// Ok None
2741
```
2842

43+
### Example 3
44+
45+
```fsharp
46+
let result =
47+
Error "bad things happened" // Result<string option, string>
48+
|> ResultOption.map remainingCharacters // Result<int option, string>
49+
50+
// Error "bad things happened"
51+
```

gitbook/resultOption/map2.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.map2
1+
# ResultOption.map2
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -11,6 +11,8 @@ Function Signature:
1111
-> Result<'c option, 'd>
1212
```
1313

14+
## Examples
15+
1416
### Example 1
1517

1618
Given the following function:
@@ -49,7 +51,6 @@ type CreatePostRequestDto = {
4951

5052
We can then create a function transforming a `CreatePostRequestDto` to a `CreatePostRequest`, using `Option.traverseResult`, `ResultOption.map2`, and `Result.map2`:
5153

52-
5354
```fsharp
5455
// CreatePostRequestDto -> Result<CreatePostRequest, string>
5556
let toCreatePostRequest (dto : CreatePostRequestDto) =

gitbook/resultOption/map3.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## ResultOption.map3
1+
# ResultOption.map3
22

33
Namespace: `FsToolkit.ErrorHandling`
44

@@ -12,6 +12,8 @@ Function Signature:
1212
-> Result<'d option, 'e>
1313
```
1414

15+
## Examples
16+
1517
### Example 1
1618

1719
Given the following function:
@@ -26,4 +28,3 @@ Then using `ResultOption.map3`, we can do the following:
2628
ResultOption.map3 add (Ok (Some 30)) (Ok (Some 10)) (Ok (Some 2))
2729
// Ok (Some 42)
2830
```
29-

gitbook/resultOption/operators.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
## ResultOption Infix Operators
1+
# ResultOption Infix Operators
22

33
Namespace: `FsToolkit.ErrorHandling.Operator.ResultOption`
44

55
FsToolkit.ErrorHandling provides the standard infix operators for the `map` (`<!>`), `apply` (`<*>`), and `bind` (`>>=`) functions of the `Result<Option<_>,_>` type.
66

77
In addition to these, it also offers an another infix operator `<*^>` for usage with normal `Result` values (without an inner `Option`). It has the following function signature:
88

9-
```
9+
``` fsharp
1010
Result<('a -> 'b) option, 'c> -> Result<'a, 'c>
1111
-> Result<'b option, 'c>
1212
```

gitbook/resultOption/zip.md

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

0 commit comments

Comments
 (0)