This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_flat_map_example_test.go
More file actions
120 lines (103 loc) · 3.06 KB
/
4_flat_map_example_test.go
File metadata and controls
120 lines (103 loc) · 3.06 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package mu_test
import (
"errors"
"fmt"
"github.com/appthrust/mu"
)
func ExampleOption_FlatMap() {
// FlatMap with Some value
some42 := mu.Some(42)
result := some42.FlatMap(func(x int) mu.Option[int] {
if x > 40 {
return mu.Some(x * 2)
}
return mu.None[int]()
})
fmt.Println("Some(42).FlatMap(x*2 if >40):", result.OrZero())
// FlatMap with None value
none := mu.None[int]()
result2 := none.FlatMap(func(x int) mu.Option[int] {
return mu.Some(x * 2)
})
fmt.Println("None.FlatMap(x*2):", result2.IsNone())
// Output:
// Some(42).FlatMap(x*2 if >40): 84
// None.FlatMap(x*2): true
}
func ExampleEither_FlatMapLeft() {
// FlatMapLeft with Left value
left := mu.Left[string, int]("error")
result := left.FlatMapLeft(func(s string) mu.Either[string, int] {
return mu.Left[string, int]("processed: " + s)
})
fmt.Println("Left(\"error\").FlatMapLeft():", result.LeftOrZero())
// FlatMapLeft with Right value
right := mu.Right[string, int](42)
result2 := right.FlatMapLeft(func(s string) mu.Either[string, int] {
return mu.Left[string, int]("processed: " + s)
})
fmt.Println("Right(42).FlatMapLeft():", result2.OrZero())
// Output:
// Left("error").FlatMapLeft(): processed: error
// Right(42).FlatMapLeft(): 42
}
func ExampleEither_FlatMap() {
// FlatMap with Right value
right := mu.Right[string, int](42)
result := right.FlatMap(func(x int) mu.Either[string, int] {
if x > 40 {
return mu.Right[string, int](x * 2)
}
return mu.Left[string, int]("too small")
})
fmt.Println("Right(42).FlatMap(x*2 if >40):", result.OrZero())
// FlatMap with Left value
left := mu.Left[string, int]("error")
result2 := left.FlatMap(func(x int) mu.Either[string, int] {
return mu.Right[string, int](x * 2)
})
fmt.Println("Left(\"error\").FlatMap():", result2.LeftOrZero())
// Output:
// Right(42).FlatMap(x*2 if >40): 84
// Left("error").FlatMap(): error
}
func ExampleResult_FlatMap() {
// FlatMap with Ok value
ok := mu.Ok(42)
result := ok.FlatMap(func(x int) mu.Result[int] {
if x > 40 {
return mu.Ok(x * 2)
}
return mu.Err[int](errors.New("too small"))
})
fmt.Println("Ok(42).FlatMap(x*2 if >40):", result.OrZero())
// FlatMap with Err value
err := mu.Err[int](errors.New("initial error"))
result2 := err.FlatMap(func(x int) mu.Result[int] {
return mu.Ok(x * 2)
})
fmt.Println("Err.FlatMap():", result2.ErrOrZero().Error())
// Output:
// Ok(42).FlatMap(x*2 if >40): 84
// Err.FlatMap(): initial error
}
func ExampleResult_FlatMapErr() {
// FlatMapErr with Err value
err := mu.Err[int](errors.New("connection failed"))
result := err.FlatMapErr(func(e error) mu.Result[int] {
if e.Error() == "connection failed" {
return mu.Ok(42) // Recover from specific error
}
return mu.Err[int](e)
})
fmt.Println("Err(\"connection failed\").FlatMapErr():", result.OrZero())
// FlatMapErr with Ok value
ok := mu.Ok(42)
result2 := ok.FlatMapErr(func(_ error) mu.Result[int] {
return mu.Ok(0)
})
fmt.Println("Ok(42).FlatMapErr():", result2.OrZero())
// Output:
// Err("connection failed").FlatMapErr(): 42
// Ok(42).FlatMapErr(): 42
}