-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_r.go
46 lines (40 loc) · 817 Bytes
/
utils_r.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
package arrayr
import (
"reflect"
"github.com/eenti-utils/typr"
)
func getFrequency[V any](e V, fs ...*frequency[V]) (r *frequency[V], b bool) {
for _, f := range fs {
if isFrequency(e, f) {
r = f
b = true
return
}
}
return
}
func isFrequency[V any](e V, f *frequency[V]) (r bool) {
if f != nil {
r = reflect.DeepEqual(f.X, e)
}
return
}
func pairX[V any](p typr.Pair[V, int]) (r V) {
r = p.X
return
}
func doFrequencyR[V any](a ...V) (r []typr.Pair[V, int]) {
var f []*frequency[V]
for _, el := range a {
if p, exists := getFrequency(el, f...); exists {
p.Y += 1
} else {
f = append(f, &frequency[V]{X: el, Y: 1})
}
}
if len(f) > 0 {
cast := func(v *frequency[V]) (r typr.Pair[V, int]) { return typr.Pair[V, int](*v).Value() }
r = ChangeTo(cast, f...)
}
return
}