Skip to content

Commit 405265e

Browse files
authored
Added Option functions and Added Missing Documentation (#246)
* WIP * WIP * WIP doc updates * Moved some docs to a more applicable location. Added sequence and traverse option list functions * Updated zip documentation * Added unit tests
1 parent 6a4653a commit 405265e

File tree

19 files changed

+928
-17
lines changed

19 files changed

+928
-17
lines changed

gitbook/SUMMARY.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@
2929
* [ofChoice](result/ofChoice.md)
3030

3131
* Option
32-
* [bind](pr.md)
33-
* [bindNull](pr.md)
32+
* [bind](option/bind.md)
33+
* [bindNull](option/bindNull.md)
3434
* [Computation Expression](option/ce.md)
35-
* [either](pr.md)
36-
* [map](pr.md)
37-
* [zip](pr.md)
35+
* [either](option/either.md)
36+
* [map](option/map.md)
37+
* [map2](option/map2.md)
38+
* [map3](option/map3.md)
39+
* [tee Functions](option/teeFunctions.md)
40+
* [zip](option/zip.md)
3841
* Lists
39-
* [traverseResult](option/traverseResult.md)
40-
* [sequenceResult](option/sequenceResult.md)
42+
* [traverseOptionM](option/traverseOptionM.md)
43+
* [sequenceOptionM](option/sequenceOptionM.md)
44+
* [traverseVOptionM](option/traverseVOptionM.md)
45+
* [sequenceVOptionM](option/sequenceVOptionM.md)
4146
* Transforms
4247
* [ofNull](option/ofNull.md)
4348
* [ofPair](option/ofPair.md)
@@ -57,6 +62,9 @@
5762
* [Operators](resultOption/operators.md)
5863
* [zip](resultOption/zip.md)
5964
* [zipError](resultOption/zipError.md)
65+
* Lists
66+
* [traverseResult](resultOption/traverseResult.md)
67+
* [sequenceResult](resultOption/sequenceResult.md)
6068
* Transforms
6169
* [ofChoice](resultOption/ofChoice.md)
6270
* [ofOption](resultOption/ofOption.md)

gitbook/option/bind.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Option.bind
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
('TInput -> 'TOutput option) -> 'TInput option -> 'TOutput option
9+
```
10+
11+
## Examples
12+
13+
Take the following function for example
14+
15+
```fsharp
16+
// string -> int option
17+
let tryParseInt (s: string) =
18+
match Int32.TryParse(s) with
19+
| true, i -> Some i
20+
| false, _ -> None
21+
```
22+
23+
### Example 1
24+
25+
```fsharp
26+
let opt : int option =
27+
Some "123" // string option
28+
|> Option.bind tryParseInt // int option
29+
30+
// Some 123
31+
```
32+
33+
### Example 2
34+
35+
```fsharp
36+
let opt : int option =
37+
Some "Not a number" // string option
38+
|> Option.bind tryParseInt // int option
39+
40+
// None
41+
```
42+
43+
### Example 3
44+
45+
```fsharp
46+
let opt : int option =
47+
None // string option
48+
|> Option.bind tryParseInt // int option
49+
50+
// None
51+
```

gitbook/option/bindNull.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Option.bindNull
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
## Function Signature
6+
7+
```fsharp
8+
('T -> 'nullableValue) -> 'T option -> 'nullableValue option
9+
```
10+
11+
## Examples
12+
13+
14+
### Example 1
15+
16+
```fsharp
17+
open System
18+
19+
let userInput = Some 12
20+
let toNullable<'T> x = Nullable x
21+
22+
Option.bindNull toNullable userInput
23+
// Some 12
24+
```
25+
26+
### Example 2
27+
28+
```fsharp
29+
open System
30+
31+
let userInput : Option<int> = None
32+
let toNullable<'T> x = Nullable x
33+
34+
Option.bindNull toNullable userInput
35+
// None
36+
```
37+
38+
### Example 3
39+
40+
```fsharp
41+
let userInput = Some 12
42+
Option.bindNull string userInput
43+
// Some "12"
44+
```
45+
46+

gitbook/option/either.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Option.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+
('T-> 'output) -> (unit -> 'output) -> 'T option -> 'output
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
Option.either (fun x -> x * 2) (fun () -> 0) (Some 5)
19+
20+
// 10
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
Option.either (fun x -> x * 2) (fun () -> 0) None
27+
28+
// 0
29+
```
30+

gitbook/option/map.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Option.map
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the value of an option if it is `Some`. If the option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('TInput-> 'TOutput) -> 'TInput option -> 'TOutput option
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
Option.map (fun x -> x + 1) (Some 1)
19+
20+
// Some 2
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
Option.map (fun x -> x + 1) None
27+
28+
// None
29+
```
30+

gitbook/option/map2.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Option.map2
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the values of two options if they are `Some`. If either option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('TInput1 -> 'TInput2 -> 'TOutput) -> 'TInput1 option -> 'TInput2 option -> 'TOutput option
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
Option.map2 (fun x y -> x + y) (Some 1) (Some 2)
19+
20+
// Some 3
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
Option.map2 (fun x y -> x + y) (Some 1) None
27+
28+
// None
29+
```
30+
31+
### Example 3
32+
33+
```fsharp
34+
Option.map2 (fun x y -> x + y) None (Some 2)
35+
36+
// None
37+
```

gitbook/option/map3.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Option.map3
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Apply a function to the values of three options if they are `Some`. If any option is `None`, return `None`.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
('TInput1 -> 'TInput2 -> 'TInput3 -> 'TOutput) -> 'TInput1 option -> 'TInput2 option -> 'TInput3 option -> 'TOutput option
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
Option.map3 (fun x y z -> x + y + z) (Some 1) (Some 2) (Some 3)
19+
20+
// Some 6
21+
```
22+
23+
### Example 2
24+
25+
```fsharp
26+
Option.map3 (fun x y z -> x + y + z) (Some 1) (Some 2) None
27+
28+
// None
29+
```
30+
31+
### Example 3
32+
33+
```fsharp
34+
Option.map3 (fun x y z -> x + y + z) (Some 1) None (Some 3)
35+
36+
// None
37+
```
38+
39+
### Example 4
40+
41+
```fsharp
42+
Option.map3 (fun x y z -> x + y + z) None (Some 2) (Some 3)
43+
44+
// None
45+
```

gitbook/option/ofPair.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,25 @@ let opt = Option.ofPair (true, 1)
2525
let opt = Option.ofPair (false, 1)
2626
// None
2727
```
28+
29+
### Example 3
30+
31+
Instead of using this code snippet,
32+
33+
```fsharp
34+
match Int32.TryParse "12" with
35+
| true, x -> x
36+
| false, _ -> 0
37+
38+
// 12
39+
```
40+
41+
you could use `Option.ofPair` if it better suits your use case
42+
43+
```fsharp
44+
match Int32.TryParse "12" |> Option.ofPair with
45+
| Some x -> x
46+
| None -> 0
47+
48+
// 12
49+
```

gitbook/option/sequenceOptionM.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# List.sequenceOptionM
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Applies the monadic function `id` to each element in the input list, and returns the result as an option. If any element in the list is None, the entire result will be None.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
'a option list -> 'a list option
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
let myList =
19+
[
20+
Some 123
21+
Some 456
22+
Some 789
23+
]
24+
25+
List.sequenceOptionM myList
26+
// Some [123; 456; 789]
27+
```
28+
29+
### Example 2
30+
31+
```fsharp
32+
let myList =
33+
[
34+
Some 123
35+
None
36+
Some 789
37+
]
38+
39+
List.sequenceOptionM myList
40+
// None
41+
```

gitbook/option/sequenceVOptionM.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# List.sequenceVOptionM
2+
3+
Namespace: `FsToolkit.ErrorHandling`
4+
5+
Applies the monadic function `id` to each element in the input list, and returns the result as an option. If any element in the list is ValueNone, the entire result will be ValueNone.
6+
7+
## Function Signature
8+
9+
```fsharp
10+
'a voption list -> 'a list voption
11+
```
12+
13+
## Examples
14+
15+
### Example 1
16+
17+
```fsharp
18+
let myList =
19+
[
20+
ValueSome 123
21+
ValueSome 456
22+
ValueSome 789
23+
]
24+
25+
List.sequenceVOptionM myList
26+
// ValueSome [123; 456; 789]
27+
```
28+
29+
### Example 2
30+
31+
```fsharp
32+
let myList =
33+
[
34+
ValueSome 123
35+
ValueNone
36+
ValueSome 789
37+
]
38+
39+
List.sequenceVOptionM myList
40+
// ValueNone
41+
```

0 commit comments

Comments
 (0)