-
Notifications
You must be signed in to change notification settings - Fork 0
/
numbers.go
200 lines (190 loc) · 5.47 KB
/
numbers.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
package refined
type PosInt = Refined[int, Positive]
type PosInt8 = Refined[int8, Positive]
type PosInt16 = Refined[int16, Positive]
type PosInt32 = Refined[int32, Positive]
type PosInt64 = Refined[int64, Positive]
type PosUInt = Refined[uint, Positive]
type PosUInt8 = Refined[uint8, Positive]
type PosUInt16 = Refined[uint16, Positive]
type PosUInt32 = Refined[uint32, Positive]
type PosUInt64 = Refined[uint64, Positive]
type NonNegInt = Refined[int, NonNegative]
type NonNegInt8 = Refined[int8, NonNegative]
type NonNegInt16 = Refined[int16, NonNegative]
type NonNegInt32 = Refined[int32, NonNegative]
type NonNegInt64 = Refined[int64, NonNegative]
type NegInt = Refined[int, Negative]
type NegInt8 = Refined[int8, Negative]
type NegInt16 = Refined[int16, Negative]
type NegInt32 = Refined[int32, Negative]
type NegInt64 = Refined[int64, Negative]
type NonPosInt = Refined[int, NonPositive]
type NonPosInt8 = Refined[int8, NonPositive]
type NonPosInt16 = Refined[int16, NonPositive]
type NonPosInt32 = Refined[int32, NonPositive]
type NonPosInt64 = Refined[int64, NonPositive]
type PosFloat32 = Refined[float32, Positive]
type PosFloat64 = Refined[float64, Positive]
type NonNegFloat32 = Refined[float32, NonNegative]
type NonNegFloat64 = Refined[float64, NonNegative]
type NegFloat32 = Refined[float32, Negative]
type NegFloat64 = Refined[float64, Negative]
type NonPosFloat32 = Refined[float32, NonPositive]
type NonPosFloat64 = Refined[float64, NonPositive]
type Positive struct{} // x >0
type Negative struct{} // x <0
type NonNegative struct{} // x >= 0
type NonPositive struct{} // x <= 0
type LessThan[N Number, NB TNumber[N]] struct{} // x < NB
type GreaterThan[N Number, NB TNumber[N]] struct{} // x > NB
type From[N Number, NB TNumber[N]] struct{} // x >= NB
type To[N Number, NB TNumber[N]] struct{} // x <= N
type FromTo[N Number, LNB TNumber[N], RNB TNumber[N]] struct{} // LNB <= x <= RNB
type Between[N Number, LNB TNumber[N], RNB TNumber[N]] struct{} // LNB < x < RNB
type FromUntil[N Number, LNB TNumber[N], RNB TNumber[N]] struct{} // LNB <= x < RNB
type AfterTo[N Number, LNB TNumber[N], RNB TNumber[N]] struct{} // LNB < x <= RNB
func (p Positive) applyPredicate(n any) (bool, error) {
switch n := n.(type) {
case int:
return n > 0, nil
case int8:
return n > 0, nil
case int32:
return n > 0, nil
case int64:
return n > 0, nil
case float32:
return n > 0, nil
case float64:
return n > 0, nil
case uint:
return n > 0, nil
case uint8:
return n > 0, nil
case uint16:
return n > 0, nil
case uint32:
return n > 0, nil
case uint64:
return n > 0, nil
default:
return false, valueTypeError(n, p)
}
}
func (p Negative) applyPredicate(n any) (bool, error) {
switch n := n.(type) {
case int:
return n < 0, nil
case int8:
return n < 0, nil
case int32:
return n < 0, nil
case int64:
return n < 0, nil
case float32:
return n < 0, nil
case float64:
return n < 0, nil
case uint:
return n < 0, nil
case uint8:
return n < 0, nil
case uint16:
return n < 0, nil
case uint32:
return n < 0, nil
case uint64:
return n < 0, nil
default:
return false, valueTypeError(n, p)
}
}
func (p NonNegative) applyPredicate(n any) (bool, error) {
switch n := n.(type) {
case int:
return n >= 0, nil
case int8:
return n >= 0, nil
case int32:
return n >= 0, nil
case int64:
return n >= 0, nil
case float32:
return n >= 0, nil
case float64:
return n >= 0, nil
case uint:
return n >= 0, nil
case uint8:
return true, nil
case uint16:
return true, nil
case uint32:
return true, nil
case uint64:
return n >= 0, nil
default:
return false, valueTypeError(n, p)
}
}
func (p NonPositive) applyPredicate(n any) (bool, error) {
switch n := n.(type) {
case int:
return n <= 0, nil
case int8:
return n <= 0, nil
case int32:
return n <= 0, nil
case int64:
return n <= 0, nil
case float32:
return n <= 0, nil
case float64:
return n <= 0, nil
case uint:
return n <= 0, nil
case uint8:
return n <= 0, nil
case uint16:
return n <= 0, nil
case uint32:
return n <= 0, nil
case uint64:
return n <= 0, nil
default:
return false, valueTypeError(n, p)
}
}
func (p LessThan[N, NB]) applyPredicate(n any) (bool, error) {
var nb NB
return applyOn(nb.Of(), n, func(l N, r N) bool { return l > r }, p)
}
func (p GreaterThan[N, NB]) applyPredicate(n any) (bool, error) {
var nb NB
return applyOn(nb.Of(), n, func(l N, r N) bool { return l < r }, p)
}
func (p From[N, NB]) applyPredicate(n any) (bool, error) {
var nb NB
return applyOn(nb.Of(), n, func(l N, r N) bool { return l <= r }, p)
}
func (p To[N, NB]) applyPredicate(n any) (bool, error) {
var nb NB
return applyOn(nb.Of(), n, func(l N, r N) bool { return l >= r }, p)
}
func (ft Between[N, LNB, RNB]) applyPredicate(n any) (bool, error) {
var pred And[GreaterThan[N, LNB], LessThan[N, RNB]]
return pred.applyPredicate(n)
}
func (ft FromTo[N, LNB, RNB]) applyPredicate(n any) (bool, error) {
var pred And[From[N, LNB], To[N, RNB]]
return pred.applyPredicate(n)
}
func (ft FromUntil[N, LNB, RNB]) applyPredicate(n any) (bool, error) {
var pred And[From[N, LNB], LessThan[N, RNB]]
return pred.applyPredicate(n)
}
func (ft AfterTo[N, LNB, RNB]) applyPredicate(n any) (bool, error) {
var pred And[GreaterThan[N, LNB], To[N, RNB]]
return pred.applyPredicate(n)
}