-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslices.go
302 lines (275 loc) · 6.39 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package slices
type Ordered interface {
int | int32 | int16 | int8 | int64 | uint | uint32 | uint16 | uint8 | uint64 | float32 | float64 | string
}
// Clone creates a clone slice and returns it.
func Clone[T any](s []T) []T {
c := make([]T, len(s))
for i := range s {
c[i] = s[i]
}
return c
}
// Append appends an item to the slice and returns the new slice. The
// given slice is not changed.
func Append[T any](s []T, item ...T) []T {
return append(Clone(s), item...)
}
// Reverse creates a slice that is the reverse of the provided slice
// and returns it. The given slice is not changed.
func Reverse[T any](s []T) []T {
res := make([]T, len(s))
for i := 0; i < len(s); i++ {
res[i] = s[len(s)-1-i]
}
return res
}
// Splice creates a new slice that is spliced by removing or
// replacing existing elements and/or adding new elements in place.
// The given slice is not changed.
func Splice[T any](s []T, index int, cnt int, item ...T) []T {
tail := Unshift(s[index:][cnt:], item...)
s = s[:index]
return Concat(s, tail)
}
// Concat combines the contents of all the given slices. The given
// slices are not changed.
func Concat[T any](s ...[]T) []T {
totalLen := 0
for i := range s {
totalLen += len(s[i])
}
output := make([]T, 0, totalLen)
for i := range s {
output = append(output, s[i]...)
}
return output
}
// Unshift creates a new slice and prepends all the given items and
// returns it. The given slice is not changed.
func Unshift[T any](s []T, item ...T) []T {
return append(item, s...)
}
// Find finds an item in the given slice that satisfies the given
// test function.
func Find[T any](s []T, test func(item T) bool) T {
for i := range s {
if test(s[i]) {
return s[i]
}
}
var t T
return t
}
// IndexOf finds the index of the first item in the given slice that
// satisfies the given test function.
func IndexOf[T Ordered](s []T, item T) int {
for i := range s {
if s[i] == item {
return i
}
}
return -1
}
// IndexOfFunc finds the index of the first item in the given slice
// that satisfies the given test function.
func IndexOfFunc[T any](s []T, test func(item T) bool) int {
for i := range s {
if test(s[i]) {
return i
}
}
return -1
}
// Some checks is any of the items in the given slice satisfies the
// given test function.
func Some[T any](s []T, test func(item T) bool) bool {
return IndexOfFunc(s, test) >= 0
}
// Contains checks if any of the items in the given slice are equal
// to the given item.
func Contains[T Ordered](s []T, item T) bool {
for i := range s {
if s[i] == item {
return true
}
}
return false
}
// Max returns the max item in the given slice.
func Max[T Ordered](s []T) T {
if len(s) == 0 {
var t T
return t
}
max := s[0]
for i := range s {
if s[i] > max {
max = s[i]
}
}
return max
}
// Min returns the min item in the given slice.
func Min[T Ordered](s []T) T {
if len(s) == 0 {
var t T
return t
}
min := s[0]
for i := range s {
if s[i] < min {
min = s[i]
}
}
return min
}
// MaxFunc returns the max item in the given slice according to the
// given less func.
func MaxFunc[T any](s []T, less func(T, T) bool) T {
if len(s) == 0 {
var t T
return t
}
max := s[0]
for i := range s {
if less(max, s[i]) {
max = s[i]
}
}
return max
}
// MinFunc returns the min item in the given slice according to the
// given less func.
func MinFunc[T any](s []T, less func(T, T) bool) T {
if len(s) == 0 {
var t T
return t
}
min := s[0]
for i := range s {
if less(s[i], min) {
min = s[i]
}
}
return min
}
// Every checks is every item in the given slice satisfies the
// given test function.
func Every[T any](s []T, test func(item T) bool) bool {
for i := range s {
if !test(s[i]) {
return false
}
}
return true
}
// SortFunc creates a new slice that is sorted in ascending order
// according the the given less func and returns it. The given slice
// is not changed.
func SortFunc[T any](s []T, less func(a T, b T) bool) []T {
c := Clone(s)
quickSortFunc(c, 0, len(c)-1, less)
return c
}
// Sort creates a new slice that is sorted in ascending order. The
// given slice is not changed.
func Sort[T Ordered](s []T) []T {
c := Clone(s)
quickSort(c, 0, len(s)-1)
return c
}
// Filter creates a new slice that contains items from the given
// slice that satisfy the given test function and returns it. The
// given slice is not changed.
func Filter[T any](s []T, test func(item T) bool) []T {
n := make([]T, 0, len(s))
for i := range s {
if test(s[i]) {
n = append(n, s[i])
}
}
return n
}
// Map creates a new slice with items that are mapped to new values
// according to the given m function. The given slice is not
// changed.
func Map[T any, K any](s []T, m func(item T) K) []K {
k := make([]K, len(s))
for i := range s {
k[i] = m(s[i])
}
return k
}
// Reduce iterates through the given slice, reducing the items to a
// value according to the given reducer function and returns the
// reduced value. The given slice is not changed.
func Reduce[T any, K any](s []T, f func(prev K, cur T) K) K {
var k K
for i := range s {
k = f(k, s[i])
}
return k
}
// Intersection creates a new slice that contains the intersection of
// all the given slices. The given slices are not changed. All items
// in the returned slice are distinct.
func Intersection[T Ordered](s ...[]T) []T {
if len(s) == 0 {
return []T{}
}
hash := make(map[T]int)
for i := range s {
for j := range s[i] {
if hash[s[i][j]] == i {
hash[s[i][j]]++
}
}
}
result := make([]T, 0, len(hash))
for k := range hash {
if hash[k] == len(s) {
result = append(result, k)
}
}
return result
}
// Union creates a new slice that contains the union of all the given
// slices. The given slices are not changed. All items in the
// returned slice are distinct.
func Union[T Ordered](s ...[]T) []T {
if len(s) == 0 {
return []T{}
}
hash := make(map[T]struct{})
for i := range s {
for j := range s[i] {
hash[s[i][j]] = struct{}{}
}
}
output := make([]T, len(hash))
i := 0
for k := range hash {
output[i] = k
i++
}
return output
}
// Distinct creates a new slice that contains all of the distinct
// items from the given slices without duplicates.
func Distinct[T Ordered](s []T) []T {
if len(s) == 0 {
return []T{}
}
hash := make(map[T]struct{})
for i := range s {
hash[s[i]] = struct{}{}
}
output := make([]T, len(hash))
i := 0
for k := range hash {
output[i] = k
i++
}
return output
}