-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsets.go
55 lines (43 loc) · 1.2 KB
/
sets.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
package dry
import "reflect"
func SetUnify(a, b any) any {
aval := reflect.ValueOf(a)
if aval.Type().Kind() != reflect.Slice {
panic("first element is not a slice: " + aval.Type().String())
}
bval := reflect.ValueOf(b)
if bval.Type().Kind() != reflect.Slice {
panic("second element is not a slice: " + bval.Type().String())
}
if aval.Type().Elem() != bval.Type().Elem() {
panic("slices has different types")
}
pretotal := reflect.MakeMap(reflect.MapOf(aval.Type().Elem(), reflect.TypeOf(null{})))
for i := 0; i < aval.Len(); i++ {
pretotal.SetMapIndex(aval.Index(i), reflect.ValueOf(null{}))
}
for i := 0; i < bval.Len(); i++ {
pretotal.SetMapIndex(bval.Index(i), reflect.ValueOf(null{}))
}
res := reflect.New(aval.Type()).Elem()
for _, key := range pretotal.MapKeys() {
res = reflect.Append(res, key)
}
return res.Interface()
}
// пока только строчки, потом сделаю нормальный интерфейс
func SetsEqual(a, b []string) bool {
// If one is nil, the other must also be nil.
if (a == nil) != (b == nil) {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}