-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
either.go
189 lines (154 loc) · 4.21 KB
/
either.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package mo
import "fmt"
var eitherShouldBeLeftOrRight = fmt.Errorf("either should be Left or Right")
var eitherMissingLeftValue = fmt.Errorf("no such Left value")
var eitherMissingRightValue = fmt.Errorf("no such Right value")
// Left builds the left side of the Either struct, as opposed to the Right side.
func Left[L any, R any](value L) Either[L, R] {
return Either[L, R]{
isLeft: true,
left: value,
}
}
// Right builds the right side of the Either struct, as opposed to the Left side.
func Right[L any, R any](value R) Either[L, R] {
return Either[L, R]{
isLeft: false,
right: value,
}
}
// Either respresents a value of 2 possible types.
// An instance of Either is an instance of either A or B.
type Either[L any, R any] struct {
isLeft bool
left L
right R
}
// IsLeft returns true if Either is an instance of Left.
func (e Either[L, R]) IsLeft() bool {
return e.isLeft
}
// IsRight returns true if Either is an instance of Right.
func (e Either[L, R]) IsRight() bool {
return !e.isLeft
}
// Left returns left value of a Either struct.
func (e Either[L, R]) Left() (L, bool) {
if e.IsLeft() {
return e.left, true
}
return empty[L](), false
}
// Right returns right value of a Either struct.
func (e Either[L, R]) Right() (R, bool) {
if e.IsRight() {
return e.right, true
}
return empty[R](), false
}
// MustLeft returns left value of a Either struct or panics.
func (e Either[L, R]) MustLeft() L {
if !e.IsLeft() {
panic(eitherMissingLeftValue)
}
return e.left
}
// MustRight returns right value of a Either struct or panics.
func (e Either[L, R]) MustRight() R {
if !e.IsRight() {
panic(eitherMissingRightValue)
}
return e.right
}
// Unpack returns all values
func (e Either[L, R]) Unpack() (L, R) {
return e.left, e.right
}
// LeftOrElse returns left value of a Either struct or fallback.
func (e Either[L, R]) LeftOrElse(fallback L) L {
if e.IsLeft() {
return e.left
}
return fallback
}
// RightOrElse returns right value of a Either struct or fallback.
func (e Either[L, R]) RightOrElse(fallback R) R {
if e.IsRight() {
return e.right
}
return fallback
}
// LeftOrEmpty returns left value of a Either struct or empty value.
func (e Either[L, R]) LeftOrEmpty() L {
if e.IsLeft() {
return e.left
}
return empty[L]()
}
// RightOrEmpty returns right value of a Either struct or empty value.
func (e Either[L, R]) RightOrEmpty() R {
if e.IsRight() {
return e.right
}
return empty[R]()
}
// Swap returns the left value in Right and vice versa.
func (e Either[L, R]) Swap() Either[R, L] {
if e.IsLeft() {
return Right[R, L](e.left)
}
return Left[R, L](e.right)
}
// ForEach executes the given side-effecting function, depending of value is Left or Right.
func (e Either[L, R]) ForEach(leftCb func(L), rightCb func(R)) {
if e.IsLeft() {
leftCb(e.left)
} else if e.IsRight() {
rightCb(e.right)
}
}
// Match executes the given function, depending of value is Left or Right, and returns result.
func (e Either[L, R]) Match(onLeft func(L) Either[L, R], onRight func(R) Either[L, R]) Either[L, R] {
if e.IsLeft() {
return onLeft(e.left)
} else if e.IsRight() {
return onRight(e.right)
}
panic(eitherShouldBeLeftOrRight)
}
// MapLeft executes the given function, if Either is of type Left, and returns result.
func (e Either[L, R]) MapLeft(mapper func(L) Either[L, R]) Either[L, R] {
if e.IsLeft() {
return mapper(e.left)
} else if e.IsRight() {
return Right[L, R](e.right)
}
panic(eitherShouldBeLeftOrRight)
}
// MapRight executes the given function, if Either is of type Right, and returns result.
func (e Either[L, R]) MapRight(mapper func(R) Either[L, R]) Either[L, R] {
if e.isLeft {
return Left[L, R](e.left)
} else if e.IsRight() {
return mapper(e.right)
}
panic(eitherShouldBeLeftOrRight)
}
// leftValue returns left value of a Either struct.(implementation of Foldable interface)
//
//nolint:unused
func (e Either[L, R]) leftValue() L {
return e.left
}
// rightValue returns right value of a Either struct.(implementation of Foldable interface)
//
//nolint:unused
func (e Either[L, R]) rightValue() R {
return e.right
}
// hasLeft returns true if the Result represents an error state.
//
//nolint:unused
func (e Either[L, R]) hasLeftValue() bool {
return e.isLeft
}