-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathfold_example_test.go
63 lines (50 loc) · 1.68 KB
/
fold_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package mo
import (
"errors"
"fmt"
)
func ExampleFold_result() {
res1 := Result[int]{value: 42, isErr: false, err: nil}
res2 := Result[int]{value: 0, isErr: true, err: errors.New("error")}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](res1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](res2, successFunc, failureFunc))
// Output:
// Success with value 42
// Failure with error error
}
func ExampleFold_either() {
either1 := Either[error, int]{isLeft: false, right: 42}
either2 := Either[error, int]{isLeft: true, left: errors.New("either error")}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](either1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](either2, successFunc, failureFunc))
// Output:
// Success with value 42
// Failure with error either error
}
func ExampleFold_option() {
option1 := Option[int]{isPresent: true, value: 42}
option2 := Option[int]{isPresent: false}
successFunc := func(val int) string {
return fmt.Sprintf("Success with value %d", val)
}
failureFunc := func(err error) string {
return fmt.Sprintf("Failure with error %s", err)
}
fmt.Println(Fold[error, int, string](option1, successFunc, failureFunc))
fmt.Println(Fold[error, int, string](option2, successFunc, failureFunc))
// Output:
// Success with value 42
// Failure with error no such element
}