-
-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathcondition.go
More file actions
155 lines (127 loc) · 3.29 KB
/
condition.go
File metadata and controls
155 lines (127 loc) · 3.29 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
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
package lo
// Ternary is a single line if/else statement.
// Take care to avoid dereferencing potentially nil pointers in your A/B expressions, because they are both evaluated. See TernaryF to avoid this problem.
// Play: https://go.dev/play/p/t-D7WBL44h2
func Ternary[T any](condition bool, ifOutput, elseOutput T) T {
if condition {
return ifOutput
}
return elseOutput
}
// TernaryF is a single line if/else statement whose options are functions.
// Play: https://go.dev/play/p/AO4VW20JoqM
func TernaryF[T any](condition bool, ifFunc, elseFunc func() T) T {
if condition {
return ifFunc()
}
return elseFunc()
}
// Perf: value receivers (not pointer) allow the compiler to fully inline the entire
// If().ElseIf().Else() chain, eliminating all function call overhead.
type ifElse[T any] struct {
result T
done bool
}
// If is a single line if/else statement.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func If[T any](condition bool, result T) ifElse[T] { //nolint:revive
if condition {
return ifElse[T]{result, true}
}
var t T
return ifElse[T]{t, false}
}
// IfF is a single line if/else statement whose options are functions.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func IfF[T any](condition bool, resultF func() T) ifElse[T] { //nolint:revive
if condition {
return ifElse[T]{resultF(), true}
}
var t T
return ifElse[T]{t, false}
}
// ElseIf.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i ifElse[T]) ElseIf(condition bool, result T) ifElse[T] {
if !i.done && condition {
i.result = result
i.done = true
}
return i
}
// ElseIfF.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i ifElse[T]) ElseIfF(condition bool, resultF func() T) ifElse[T] {
if !i.done && condition {
i.result = resultF()
i.done = true
}
return i
}
// Else.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i ifElse[T]) Else(result T) T {
if i.done {
return i.result
}
return result
}
// ElseF.
// Play: https://go.dev/play/p/WSw3ApMxhyW
func (i ifElse[T]) ElseF(resultF func() T) T {
if i.done {
return i.result
}
return resultF()
}
// Perf: value receivers (not pointer) allow the compiler to fully inline the entire
// Switch().Case().Default() chain, eliminating all function call overhead.
type switchCase[T comparable, R any] struct {
predicate T
result R
done bool
}
// Switch is a pure functional switch/case/default statement.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func Switch[T comparable, R any](predicate T) switchCase[T, R] { //nolint:revive
var result R
return switchCase[T, R]{
predicate,
result,
false,
}
}
// Case.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s switchCase[T, R]) Case(val T, result R) switchCase[T, R] {
if !s.done && s.predicate == val {
s.result = result
s.done = true
}
return s
}
// CaseF.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s switchCase[T, R]) CaseF(val T, callback func() R) switchCase[T, R] {
if !s.done && s.predicate == val {
s.result = callback()
s.done = true
}
return s
}
// Default.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s switchCase[T, R]) Default(result R) R {
if !s.done {
s.result = result
}
return s.result
}
// DefaultF.
// Play: https://go.dev/play/p/TGbKUMAeRUd
func (s switchCase[T, R]) DefaultF(callback func() R) R {
if !s.done {
s.result = callback()
}
return s.result
}