Repository navigation
Expand file tree
/
Copy pathslice.go
More file actions
226 lines (188 loc) · 5.05 KB
/
Copy pathslice.go
File metadata and controls
226 lines (188 loc) · 5.05 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package sugar
// Contains returns true if an element is present in a collection.
func Contains[T comparable](collection []T, element T) bool {
for _, item := range collection {
if item == element {
return true
}
}
return false
}
// ContainsBy returns true if predicate function returns true for any element in collection.
func ContainsBy[T any](collection []T, predicate func(T) bool) bool {
for _, item := range collection {
if predicate(item) {
return true
}
}
return false
}
// Remove removes the first occurrence of element from slice.
func Remove[T comparable](collection []T, element T) []T {
for i, val := range collection {
if element == val {
return append(collection[:i], collection[i+1:]...)
}
}
return collection
}
// RemoveAll removes all occurrences of element from slice.
func RemoveAll[T comparable](collection []T, element T) []T {
result := make([]T, 0, len(collection))
for _, val := range collection {
if val != element {
result = append(result, val)
}
}
return result
}
// Drop creates a slice with n elements dropped from the beginning.
func Drop[T any](collection []T, n int) []T {
if n <= 0 {
return append([]T(nil), collection...)
}
if n >= len(collection) {
return []T{}
}
return append([]T(nil), collection[n:]...)
}
// DropRight creates a slice with n elements dropped from the end.
func DropRight[T any](collection []T, n int) []T {
if n <= 0 {
return append([]T(nil), collection...)
}
if n >= len(collection) {
return []T{}
}
return append([]T(nil), collection[:len(collection)-n]...)
}
// DropWhile creates a slice excluding elements dropped from the beginning.
func DropWhile[T any](collection []T, predicate func(T) bool) []T {
i := 0
for ; i < len(collection); i++ {
if !predicate(collection[i]) {
break
}
}
result := make([]T, 0, len(collection)-i)
return append(result, collection[i:]...)
}
// DropRightWhile creates a slice excluding elements dropped from the end.
func DropRightWhile[T any](collection []T, predicate func(T) bool) []T {
i := len(collection) - 1
for ; i >= 0; i-- {
if !predicate(collection[i]) {
break
}
}
result := make([]T, 0, i+1)
return append(result, collection[:i+1]...)
}
// Compact creates a slice with all zero values removed.
func Compact[T comparable](collection []T) []T {
var zero T
result := make([]T, 0, len(collection))
for _, item := range collection {
if item != zero {
result = append(result, item)
}
}
return result
}
// Union creates an array of unique values from all given arrays.
func Union[T comparable](collections ...[]T) []T {
seen := make(map[T]struct{})
result := make([]T, 0)
for _, collection := range collections {
for _, item := range collection {
if _, ok := seen[item]; !ok {
seen[item] = struct{}{}
result = append(result, item)
}
}
}
return result
}
// Intersection creates an array of unique values that are included in all given arrays.
func Intersection[T comparable](collections ...[]T) []T {
if len(collections) == 0 {
return []T{}
}
if len(collections) == 1 {
return Uniq(collections[0])
}
// Count occurrences
counts := make(map[T]int)
for _, collection := range collections {
seen := make(map[T]struct{})
for _, item := range collection {
if _, ok := seen[item]; !ok {
seen[item] = struct{}{}
counts[item]++
}
}
}
// Find items that appear in all collections
result := make([]T, 0)
for item, count := range counts {
if count == len(collections) {
result = append(result, item)
}
}
return result
}
// Difference creates an array of values from the first array that are not included in the other arrays.
func Difference[T comparable](collection []T, others ...[]T) []T {
excluded := make(map[T]struct{})
for _, other := range others {
for _, item := range other {
excluded[item] = struct{}{}
}
}
result := make([]T, 0, len(collection))
for _, item := range collection {
if _, ok := excluded[item]; !ok {
result = append(result, item)
}
}
return result
}
// Concat creates a new array concatenating additional arrays.
func Concat[T any](collections ...[]T) []T {
totalLen := 0
for _, collection := range collections {
totalLen += len(collection)
}
result := make([]T, 0, totalLen)
for _, collection := range collections {
result = append(result, collection...)
}
return result
}
// Without creates an array excluding all given values.
func Without[T comparable](collection []T, exclude ...T) []T {
excludeSet := make(map[T]struct{}, len(exclude))
for _, item := range exclude {
excludeSet[item] = struct{}{}
}
result := make([]T, 0, len(collection))
for _, item := range collection {
if _, ok := excludeSet[item]; !ok {
result = append(result, item)
}
}
return result
}
// Partition creates an array of elements split into two groups.
func Partition[T any](collection []T, predicate func(T) bool) ([]T, []T) {
truthy := make([]T, 0)
falsy := make([]T, 0)
for _, item := range collection {
if predicate(item) {
truthy = append(truthy, item)
} else {
falsy = append(falsy, item)
}
}
return truthy, falsy
}