-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.go
123 lines (103 loc) · 2.23 KB
/
number.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
package cols
// Number are interface that can be fulfilled by all int and float in golang
type Number interface {
~int | ~int32 | ~int64 | ~float32 | ~float64
}
// SumBy will return map that have sum of each group.
// Will be usefull if you want to create subtotal
func SumBy[T any, N Number, K comparable](
in []T,
keyFun func(T) K,
numFun func(T) N,
) map[K]N {
map1 := GroupBy(in, keyFun)
map2 := make(map[K]N, len(map1))
for k, l := range map1 {
map2[k] = Sum(l, numFun)
}
return map2
}
// Sum will return sum of every number returned by function
func Sum[T any, N Number](in []T, fun func(T) N) N {
return SumNumber(Map(in, fun))
}
// Max will return higest of every number returned by function
func Max[T any, N Number](in []T, fun func(T) N) N {
return MaxNumber(Map(in, fun))
}
// Min will return lowest of every number returned by function
func Min[T any, N Number](in []T, fun func(T) N) N {
return MinNumber(Map(in, fun))
}
// MaxStruct will return item from slice that have highest number returned by function
func MaxStruct[T any, N Number](in []T, fun func(T) N) T {
var res T
if len(in) == 0 {
return res
}
res = in[0]
max := fun(res)
for _, v := range in {
mv := fun(v)
if fun(v) > max {
res = v
max = mv
}
}
return res
}
// MinStruct will return item from slice that have lowest number returned by function
func MinStruct[T any, N Number](in []T, fun func(T) N) T {
var res T
if len(in) == 0 {
return res
}
res = in[0]
min := fun(res)
for _, v := range in {
mv := fun(v)
if fun(v) < min {
res = v
min = mv
}
}
return res
}
// SumNumber will return sum of every number in input
func SumNumber[T Number](in []T) T {
var res T
for _, v := range in {
res += v
}
return res
}
// MaxNumber will return the higest number in input
func MaxNumber[T Number](in []T) T {
var res T
if len(in) == 0 {
return res
}
f := moreThan[T]
return Reduce(in, f, in[0])
}
// MinNumber will return the higest number in input
func MinNumber[T Number](in []T) T {
var res T
if len(in) == 0 {
return res
}
f := lessThan[T]
return Reduce(in, f, in[0])
}
func lessThan[T Number](a, b T) T {
if a < b {
return a
}
return b
}
func moreThan[T Number](a, b T) T {
if a > b {
return a
}
return b
}