-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.go
112 lines (101 loc) · 2.2 KB
/
misc.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
package main
func mapSlice[T any, M any](a []T, f func(T) M) []M {
n := make([]M, len(a))
for i, e := range a {
n[i] = f(e)
}
return n
}
func mapifySlice[E any, I comparable](a []E, f func(E) I) map[I]E {
n := make(map[I]E)
for _, e := range a {
n[f(e)] = e
}
return n
}
func selectMap[E any, I comparable](m map[I]E, filter func(I, E) bool) map[I]E {
n := make(map[I]E)
for i, e := range m {
if filter(i, e) {
n[i] = e
}
}
return n
}
func selectMapByValue[Key comparable, Value any](m map[Key]Value, selector func(Value) bool) map[Key]Value {
return selectMap(m, func(_ Key, value Value) bool {
return selector(value)
})
}
func selectSlice[Element any](slice []Element, test func(Element) bool) []Element {
var newSlice []Element
for _, element := range slice {
if test(element) {
newSlice = append(newSlice, element)
}
}
return newSlice
}
func contains[E comparable](array []E, item E) bool {
if array == nil {
return false
}
for _, s := range array {
if item == s {
return true
}
}
return false
}
func containsOnConditions[T interface{}](values []T, searched T, conditions ...func(t1 T, t2 T) bool) bool {
for _, current := range values {
accept := true
for _, condition := range conditions {
accept = accept && condition(current, searched)
}
if accept {
return true
}
}
return false
}
func intersect[E comparable](elementsA []E, elementsB []E) (both []E, onlyA []E, onlyB []E) {
for _, elementA := range elementsA {
foundBoth := false
for _, elementB := range elementsB {
if elementA == elementB {
both = append(both, elementA)
foundBoth = true
}
}
if !foundBoth {
onlyA = append(onlyA, elementA)
}
}
for _, elementB := range elementsB {
foundBoth := false
for _, elementA := range elementsA {
if elementA == elementB {
foundBoth = true
}
}
if !foundBoth {
onlyB = append(onlyB, elementB)
}
}
return both, onlyA, onlyB
}
func keys[Key comparable, Element any](m map[Key]Element) []Key {
var ks []Key
for k := range m {
ks = append(ks, k)
}
return ks
}
func values[Key comparable, Element any](m map[Key]Element) []Element {
var es []Element
for _, e := range m {
es = append(es, e)
}
return es
}