-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
slices.go
131 lines (117 loc) · 3.13 KB
/
slices.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
124
125
126
127
128
129
130
131
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"slices"
"strings"
)
// JoinStrings returns a string that is all the elements in the slice `T[]` joined by `sep`
// This being generic allows for the usage of custom string times, without having to convert
// the elements to a string to be passed into `strings.Join`.
func JoinStrings[T ~string](elems []T, sep string) T {
switch len(elems) {
case 0:
return ""
case 1:
return elems[0]
}
n := len(sep) * (len(elems) - 1)
for i := 0; i < len(elems); i++ {
n += len(elems[i])
}
var b strings.Builder
b.Grow(n)
b.WriteString(string(elems[0]))
for _, s := range elems[1:] {
b.WriteString(sep)
b.WriteString(string(s))
}
return T(b.String())
}
// Deduplicate deduplicates list of comparable values.
func Deduplicate[T comparable](in []T) []T {
if len(in) <= 1 {
return in
}
out := make([]T, 0, len(in))
seen := make(map[T]struct{}, len(in))
for _, val := range in {
if _, ok := seen[val]; !ok {
out = append(out, val)
seen[val] = struct{}{}
}
}
return out
}
// DeduplicateAny deduplicates list of any values with compare function.
func DeduplicateAny[T any](in []T, compare func(T, T) bool) []T {
if len(in) <= 1 {
return in
}
out := make([]T, 0, len(in))
for _, val := range in {
var seen bool
for _, outVal := range out {
if compare(val, outVal) {
seen = true
break
}
}
if !seen {
out = append(out, val)
}
}
return out
}
// ContainSameUniqueElements returns true if the input slices contain the same
// unique elements. Ordering and duplicates are ignored.
func ContainSameUniqueElements[S ~[]E, E comparable](s1, s2 S) bool {
s1Dedup := Deduplicate(s1)
s2Dedup := Deduplicate(s2)
if len(s1Dedup) != len(s2Dedup) {
return false
}
for i := range s1Dedup {
if !slices.Contains(s2Dedup, s1Dedup[i]) {
return false
}
}
return true
}
// Any checks if any element of slice satisfy given predicate. If the slice is empty, it returns false.
func Any[S ~[]E, E any](s S, predicate func(E) bool) bool {
for _, e := range s {
if predicate(e) {
return true
}
}
return false
}
// All checks if all elements of slice satisfy given predicate. If the slice is empty, it returns true.
func All[S ~[]E, E any](s S, predicate func(E) bool) bool {
for _, e := range s {
if !predicate(e) {
return false
}
}
return true
}
// CountBy counts the occurrences of each element in a slice based on a given mapper function.
func CountBy[S ~[]E, E any](elements S, mapper func(E) string) map[string]int {
out := make(map[string]int)
for _, elem := range elements {
key := mapper(elem)
out[key] += 1
}
return out
}