-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
either4.go
285 lines (243 loc) · 7.22 KB
/
either4.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package mo
import "fmt"
const (
either4ArgId1 = iota
either4ArgId2
either4ArgId3
either4ArgId4
)
var (
either4InvalidArgumentId = fmt.Errorf("either4 argument should be between 1 and 4")
either4MissingArg1 = fmt.Errorf("either4 doesn't contain expected argument 1")
either4MissingArg2 = fmt.Errorf("either4 doesn't contain expected argument 2")
either4MissingArg3 = fmt.Errorf("either4 doesn't contain expected argument 3")
either4MissingArg4 = fmt.Errorf("either4 doesn't contain expected argument 4")
)
// NewEither4Arg1 builds the first argument of the Either4 struct.
func NewEither4Arg1[T1 any, T2 any, T3 any, T4 any](value T1) Either4[T1, T2, T3, T4] {
return Either4[T1, T2, T3, T4]{
argId: either4ArgId1,
arg1: value,
}
}
// NewEither4Arg2 builds the second argument of the Either4 struct.
func NewEither4Arg2[T1 any, T2 any, T3 any, T4 any](value T2) Either4[T1, T2, T3, T4] {
return Either4[T1, T2, T3, T4]{
argId: either4ArgId2,
arg2: value,
}
}
// NewEither4Arg3 builds the third argument of the Either4 struct.
func NewEither4Arg3[T1 any, T2 any, T3 any, T4 any](value T3) Either4[T1, T2, T3, T4] {
return Either4[T1, T2, T3, T4]{
argId: either4ArgId3,
arg3: value,
}
}
// NewEither4Arg4 builds the fourth argument of the Either4 struct.
func NewEither4Arg4[T1 any, T2 any, T3 any, T4 any](value T4) Either4[T1, T2, T3, T4] {
return Either4[T1, T2, T3, T4]{
argId: either4ArgId4,
arg4: value,
}
}
// Either4 respresents a value of 4 possible types.
// An instance of Either4 is an instance of either T1, T2, T3 or T4.
type Either4[T1 any, T2 any, T3 any, T4 any] struct {
argId int8
arg1 T1
arg2 T2
arg3 T3
arg4 T4
}
// IsArg1 returns true if Either4 uses the first argument.
func (e Either4[T1, T2, T3, T4]) IsArg1() bool {
return e.argId == either4ArgId1
}
// IsArg2 returns true if Either4 uses the second argument.
func (e Either4[T1, T2, T3, T4]) IsArg2() bool {
return e.argId == either4ArgId2
}
// IsArg3 returns true if Either4 uses the third argument.
func (e Either4[T1, T2, T3, T4]) IsArg3() bool {
return e.argId == either4ArgId3
}
// IsArg4 returns true if Either4 uses the fourth argument.
func (e Either4[T1, T2, T3, T4]) IsArg4() bool {
return e.argId == either4ArgId4
}
// Arg1 returns the first argument of a Either4 struct.
func (e Either4[T1, T2, T3, T4]) Arg1() (T1, bool) {
if e.IsArg1() {
return e.arg1, true
}
return empty[T1](), false
}
// Arg2 returns the second argument of a Either4 struct.
func (e Either4[T1, T2, T3, T4]) Arg2() (T2, bool) {
if e.IsArg2() {
return e.arg2, true
}
return empty[T2](), false
}
// Arg3 returns the third argument of a Either4 struct.
func (e Either4[T1, T2, T3, T4]) Arg3() (T3, bool) {
if e.IsArg3() {
return e.arg3, true
}
return empty[T3](), false
}
// Arg4 returns the fourth argument of a Either4 struct.
func (e Either4[T1, T2, T3, T4]) Arg4() (T4, bool) {
if e.IsArg4() {
return e.arg4, true
}
return empty[T4](), false
}
// MustArg1 returns the first argument of a Either4 struct or panics.
func (e Either4[T1, T2, T3, T4]) MustArg1() T1 {
if !e.IsArg1() {
panic(either4MissingArg1)
}
return e.arg1
}
// MustArg2 returns the second argument of a Either4 struct or panics.
func (e Either4[T1, T2, T3, T4]) MustArg2() T2 {
if !e.IsArg2() {
panic(either4MissingArg2)
}
return e.arg2
}
// MustArg3 returns the third argument of a Either4 struct or panics.
func (e Either4[T1, T2, T3, T4]) MustArg3() T3 {
if !e.IsArg3() {
panic(either4MissingArg3)
}
return e.arg3
}
// MustArg4 returns the fourth argument of a Either4 struct or panics.
func (e Either4[T1, T2, T3, T4]) MustArg4() T4 {
if !e.IsArg4() {
panic(either4MissingArg4)
}
return e.arg4
}
// Unpack returns all values
func (e Either4[T1, T2, T3, T4]) Unpack() (T1, T2, T3, T4) {
return e.arg1, e.arg2, e.arg3, e.arg4
}
// Arg1OrElse returns the first argument of a Either4 struct or fallback.
func (e Either4[T1, T2, T3, T4]) Arg1OrElse(fallback T1) T1 {
if e.IsArg1() {
return e.arg1
}
return fallback
}
// Arg2OrElse returns the second argument of a Either4 struct or fallback.
func (e Either4[T1, T2, T3, T4]) Arg2OrElse(fallback T2) T2 {
if e.IsArg2() {
return e.arg2
}
return fallback
}
// Arg3OrElse returns the third argument of a Either4 struct or fallback.
func (e Either4[T1, T2, T3, T4]) Arg3OrElse(fallback T3) T3 {
if e.IsArg3() {
return e.arg3
}
return fallback
}
// Arg4OrElse returns the fourth argument of a Either4 struct or fallback.
func (e Either4[T1, T2, T3, T4]) Arg4OrElse(fallback T4) T4 {
if e.IsArg4() {
return e.arg4
}
return fallback
}
// Arg1OrEmpty returns the first argument of a Either4 struct or empty value.
func (e Either4[T1, T2, T3, T4]) Arg1OrEmpty() T1 {
if e.IsArg1() {
return e.arg1
}
return empty[T1]()
}
// Arg2OrEmpty returns the second argument of a Either4 struct or empty value.
func (e Either4[T1, T2, T3, T4]) Arg2OrEmpty() T2 {
if e.IsArg2() {
return e.arg2
}
return empty[T2]()
}
// Arg3OrEmpty returns the third argument of a Either4 struct or empty value.
func (e Either4[T1, T2, T3, T4]) Arg3OrEmpty() T3 {
if e.IsArg3() {
return e.arg3
}
return empty[T3]()
}
// Arg4OrEmpty returns the fourth argument of a Either4 struct or empty value.
func (e Either4[T1, T2, T3, T4]) Arg4OrEmpty() T4 {
if e.IsArg4() {
return e.arg4
}
return empty[T4]()
}
// ForEach executes the given side-effecting function, depending of the argument set.
func (e Either4[T1, T2, T3, T4]) ForEach(arg1Cb func(T1), arg2Cb func(T2), arg3Cb func(T3), arg4Cb func(T4)) {
switch e.argId {
case either4ArgId1:
arg1Cb(e.arg1)
case either4ArgId2:
arg2Cb(e.arg2)
case either4ArgId3:
arg3Cb(e.arg3)
case either4ArgId4:
arg4Cb(e.arg4)
}
}
// Match executes the given function, depending of the argument set, and returns result.
func (e Either4[T1, T2, T3, T4]) Match(
onArg1 func(T1) Either4[T1, T2, T3, T4],
onArg2 func(T2) Either4[T1, T2, T3, T4],
onArg3 func(T3) Either4[T1, T2, T3, T4],
onArg4 func(T4) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4] {
switch e.argId {
case either4ArgId1:
return onArg1(e.arg1)
case either4ArgId2:
return onArg2(e.arg2)
case either4ArgId3:
return onArg3(e.arg3)
case either4ArgId4:
return onArg4(e.arg4)
}
panic(either4InvalidArgumentId)
}
// MapArg1 executes the given function, if Either4 use the first argument, and returns result.
func (e Either4[T1, T2, T3, T4]) MapArg1(mapper func(T1) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4] {
if e.IsArg1() {
return mapper(e.arg1)
}
return e
}
// MapArg2 executes the given function, if Either4 use the second argument, and returns result.
func (e Either4[T1, T2, T3, T4]) MapArg2(mapper func(T2) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4] {
if e.IsArg2() {
return mapper(e.arg2)
}
return e
}
// MapArg3 executes the given function, if Either4 use the third argument, and returns result.
func (e Either4[T1, T2, T3, T4]) MapArg3(mapper func(T3) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4] {
if e.IsArg3() {
return mapper(e.arg3)
}
return e
}
// MapArg4 executes the given function, if Either4 use the fourth argument, and returns result.
func (e Either4[T1, T2, T3, T4]) MapArg4(mapper func(T4) Either4[T1, T2, T3, T4]) Either4[T1, T2, T3, T4] {
if e.IsArg4() {
return mapper(e.arg4)
}
return e
}