forked from evergreen-ci/evergreen
/
slice.go
53 lines (49 loc) · 1.14 KB
/
slice.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
package util
import (
"fmt"
"reflect"
)
// SliceContains returns true if elt is in slice, panics if slice is not of Kind reflect.Slice
func SliceContains(slice, elt interface{}) bool {
if slice == nil {
return false
}
v := reflect.ValueOf(slice)
if v.Kind() != reflect.Slice {
panic(fmt.Sprintf("Cannot call SliceContains on a non-slice %#v of kind %#v", slice, v.Kind().String()))
}
for i := 0; i < v.Len(); i++ {
if reflect.DeepEqual(v.Index(i).Interface(), elt) {
return true
}
}
return false
}
// StringSliceIntersection returns the intersecting elements of slices a and b.
func StringSliceIntersection(a, b []string) []string {
inA := map[string]bool{}
out := []string{}
for _, elem := range a {
inA[elem] = true
}
for _, elem := range b {
if inA[elem] {
out = append(out, elem)
}
}
return out
}
// UniqueStrings takes a slice of strings and returns a new slice with duplicates removed.
// Order is preserved.
func UniqueStrings(slice []string) []string {
seen := map[string]bool{}
out := []string{}
for _, s := range slice {
if seen[s] {
continue
}
seen[s] = true
out = append(out, s)
}
return out
}