-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.go
374 lines (329 loc) · 7.28 KB
/
math.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package gfn
/* @example Max
gfn.Max([]int16{1, 5, 9, 10}...) // 10
gfn.Max("ab", "cd", "e") // "e"
gfn.Max(1.1, math.NaN(), 2.2) // 2.2
gfn.Max([]float64{math.NaN(), math.NaN(), math.NaN()}...) // NaN
*/
// Max returns the maximum value in the array. For float64 arrays, NaN values are skipped.
func Max[T Int | Uint | Float | ~string](array ...T) T {
if len(array) == 0 {
panic("array is empty")
}
res := array[0]
for _, v := range array {
if isNaN(v) {
continue
}
if isNaN(res) || v > res {
res = v
}
}
return res
}
// isNaN reports whether input is an IEEE 754 "not-a-number" value.
func isNaN[T Int | Uint | Float | ~string](x T) bool {
// IEEE 754 says that only NaNs satisfy x != x.
return x != x
}
/* @example MaxBy
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
p := gfn.MaxBy(products, func(p Product) int {
return p.amount
}) // {"orange", 30}
*/
// MaxBy returns the maximum value in the array, using the given function to transform values.
func MaxBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) T {
if len(array) == 0 {
panic("array is empty")
}
res := array[0]
value := fn(res)
for _, v := range array[1:] {
current := fn(v)
if current > value {
res = v
value = current
}
}
return res
}
/* @example Min
gfn.Min(1.1, 2.2, 3.3) // 1.1
gfn.Min([]int16{1, 5, 9, 10}...) // 1
gfn.Min(1, -1, 10) // -1
gfn.Min([]float64{1.1, math.Inf(-1), math.NaN()}...) // math.Inf(-1)
*/
// Min returns the minimum value in the array. For float64 arrays, NaN values are skipped.
func Min[T Int | Uint | Float | ~string](array ...T) T {
if len(array) == 0 {
panic("array is empty")
}
res := array[0]
for _, v := range array {
if isNaN(v) {
continue
}
if isNaN(res) || v < res {
res = v
}
}
return res
}
/* @example MinBy
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
p := gfn.MinBy(products, func(p Product) int {
return p.amount
}) // {"apple", 10}
*/
// MinBy returns the minimum value in the array, using the given function to transform values.
func MinBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) T {
if len(array) == 0 {
panic("array is empty")
}
res := array[0]
value := fn(res)
for _, v := range array[1:] {
current := fn(v)
if current < value {
res = v
value = current
}
}
return res
}
/* @example Sum
gfn.Sum([]int{1, 5, 9, 10}...) // 25
gfn.Sum(1.1, 2.2, 3.3) // 6.6
gfn.Sum("ab", "cd", "e") // "abcde"
*/
// Sum returns the sum of all values in the array.
// Be careful when using this function for float64 arrays with NaN and Inf values.
// Sum([math.NaN(), 0.5]) produces math.NaN(). Sum(math.Inf(1), math.Inf(-1)) produces math.NaN() too.
func Sum[T Int | Uint | Float | ~string | Complex](array ...T) T {
if len(array) == 0 {
panic("array is empty")
}
res := array[0]
for i, v := range array {
if i > 0 {
res += v
}
}
return res
}
/* @example SumBy
type Product struct {
name string
amount int
}
products := []Product{
{"apple", 10},
{"banana", 20},
{"orange", 30},
}
gfn.SumBy(products, func(p Product) int {
return p.amount
}) // 60
*/
// SumBy returns the sum of all values in the array after applying fn to each value.
func SumBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) U {
if len(array) == 0 {
panic("array is empty")
}
res := fn(array[0])
for _, v := range array[1:] {
res += fn(v)
}
return res
}
/* @example Abs
gfn.Abs(-1) // 1
gfn.Abs(-100.99) // 100.99
*/
// Abs returns the absolute value of x.
func Abs[T Int | Float](x T) T {
if x < 0 {
return -x
}
return x
}
/* @example DivMod
gfn.DivMod(10, 3) // (3, 1)
*/
// DivMod returns quotient and remainder of a/b.
func DivMod[T Int | Uint](a, b T) (T, T) {
return a / b, a % b
}
/* @example Mean
gfn.Mean(1, 2, 3) // 2.0
gfn.Mean([]int{1, 2, 3, 4}...) // 2.5
*/
// Mean returns the mean of all values in the array.
func Mean[T Int | Uint | Float](array ...T) float64 {
if len(array) == 0 {
panic("array is empty")
}
sum := 0.0
for _, v := range array {
sum += float64(v)
}
return sum / float64(len(array))
}
/* @example MeanBy
type Product struct {
name string
cost float64
}
products := []Product{
{"apple", 1.5},
{"banana", 2.5},
{"orange", 3.5},
{"lemon", 4.5},
}
gfn.MeanBy(products, func(p Product) float64 {
return p.cost
}) // 3.0
*/
// MeanBy returns the mean of all values in the array after applying fn to each value.
func MeanBy[T any, U Int | Uint | Float](array []T, fn func(T) U) float64 {
if len(array) == 0 {
panic("array is empty")
}
sum := 0.0
for _, v := range array {
sum += float64(fn(v))
}
return sum / float64(len(array))
}
/* @example MinMax
gfn.MinMax(1, 5, 9, 10) // 1, 10
gfn.MinMax(math.NaN(), 1.85, 2.2) // 1.85, 2.2
gfn.MinMax(math.NaN(), math.NaN(), math.NaN()) // NaN, NaN
*/
// MinMax returns the minimum and maximum value in the array. For float64 arrays, please use MinMaxFloat64.
func MinMax[T Int | Uint | Float | ~string](array ...T) (T, T) {
if len(array) == 0 {
panic("array is empty")
}
minimum := array[0]
maximum := array[0]
for _, v := range array {
if isNaN(v) {
continue
}
if isNaN(minimum) || v < minimum {
minimum = v
}
if isNaN(maximum) || v > maximum {
maximum = v
}
}
return minimum, maximum
}
/* @example MinMaxBy
type Product struct {
name string
amount int
}
products := []Product{
{"banana", 20},
{"orange", 30},
{"apple", 10},
{"grape", 50},
{"lemon", 40},
}
gfn.MinMaxBy(products, func(p Product) int {
return p.amount
}) // {"apple", 10}, {"grape", 50}
*/
// MinMaxBy returns the minimum and maximum value in the array, using the given function to transform values.
func MinMaxBy[T any, U Int | Uint | Float | ~string](array []T, fn func(T) U) (T, T) {
if len(array) == 0 {
panic("array is empty")
}
minimum := array[0]
minValue := fn(minimum)
maximum := minimum
maxValue := minValue
for _, v := range array[1:] {
current := fn(v)
if current < minValue {
minimum = v
minValue = current
}
if current > maxValue {
maximum = v
maxValue = current
}
}
return minimum, maximum
}
/* @example Mode
gfn.Mode([]int{1, 1, 5, 5, 5, 2, 2})) // 5
*/
// Mode returns the most frequent value in the array.
func Mode[T comparable](array []T) T {
if len(array) == 0 {
panic("array is empty")
}
value := array[0]
count := 1
seen := make(map[T]int)
for _, v := range array {
seen[v]++
if seen[v] > count {
value = v
count = seen[v]
}
}
return value
}
/* @example ModeBy
type Product struct {
name string
amount int
}
products := []Product{
{"banana", 20},
{"banana", 20},
{"apple", 10},
}
gfn.ModeBy(products, func(p Product) int {
return p.amount
}) // {"banana", 20}
*/
// ModeBy returns the most frequent value in the array, using the given function to transform values.
func ModeBy[T any, U comparable](array []T, fn func(T) U) T {
if len(array) == 0 {
panic("array is empty")
}
value := array[0]
count := 1
seen := make(map[U]int)
for _, v := range array {
current := fn(v)
seen[current]++
if seen[current] > count {
value = v
count = seen[current]
}
}
return value
}