Debian Codegraph @

slices/slices.go

raw symbols SCIP
1 package slices
2
3 import (
4 "golang.org/x/exp/constraints"
5 "golang.org/x/exp/slices"
6 )
7
8 // This file contains the new functions that do not live in the official slices package.
9
10 func Some[T any](slice []T, test func(T) bool) bool {
11 for _, value := range slice {
12 if test(value) {
13 return true
14 }
15 }
16
17 return false
18 }
19
20 func Every[T any](slice []T, test func(T) bool) bool {
21 for _, value := range slice {
22 if !test(value) {
23 return false
24 }
25 }
26
27 return true
28 }
29
30 // Produces a new slice, leaves the input slice untouched.
31 func Map[T any, V any](slice []T, f func(T) V) []V {
32 result := make([]V, 0, len(slice))
33 for _, value := range slice {
34 result = append(result, f(value))
35 }
36
37 return result
38 }
39
40 // Produces a new slice, leaves the input slice untouched.
41 func MapWithIndex[T any, V any](slice []T, f func(T, int) V) []V {
42 result := make([]V, 0, len(slice))
43 for i, value := range slice {
44 result = append(result, f(value, i))
45 }
46
47 return result
48 }
49
50 func TryMap[T any, V any](slice []T, f func(T) (V, error)) ([]V, error) {
51 result := make([]V, 0, len(slice))
52 for _, value := range slice {
53 output, err := f(value)
54 if err != nil {
55 return nil, err
56 }
57 result = append(result, output)
58 }
59
60 return result, nil
61 }
62
63 func TryMapWithIndex[T any, V any](slice []T, f func(T, int) (V, error)) ([]V, error) {
64 result := make([]V, 0, len(slice))
65 for i, value := range slice {
66 output, err := f(value, i)
67 if err != nil {
68 return nil, err
69 }
70 result = append(result, output)
71 }
72
73 return result, nil
74 }
75
76 // Produces a new slice, leaves the input slice untouched.
77 func FlatMap[T any, V any](slice []T, f func(T) []V) []V {
78 // impossible to know how long this slice will be in the end but the length
79 // of the original slice is the lower bound
80 result := make([]V, 0, len(slice))
81 for _, value := range slice {
82 result = append(result, f(value)...)
83 }
84
85 return result
86 }
87
88 func FlatMapWithIndex[T any, V any](slice []T, f func(T, int) []V) []V {
89 // impossible to know how long this slice will be in the end but the length
90 // of the original slice is the lower bound
91 result := make([]V, 0, len(slice))
92 for i, value := range slice {
93 result = append(result, f(value, i)...)
94 }
95
96 return result
97 }
98
99 func Flatten[T any](slice [][]T) []T {
100 result := make([]T, 0, len(slice))
101 for _, subSlice := range slice {
102 result = append(result, subSlice...)
103 }
104 return result
105 }
106
107 func MapInPlace[T any](slice []T, f func(T) T) {
108 for i, value := range slice {
109 slice[i] = f(value)
110 }
111 }
112
113 // Produces a new slice, leaves the input slice untouched.
114 func Filter[T any](slice []T, test func(T) bool) []T {
115 result := make([]T, 0, len(slice))
116 for _, element := range slice {
117 if test(element) {
118 result = append(result, element)
119 }
120 }
121 return result
122 }
123
124 // Produces a new slice, leaves the input slice untouched.
125 func FilterWithIndex[T any](slice []T, f func(T, int) bool) []T {
126 result := make([]T, 0, len(slice))
127 for i, value := range slice {
128 if f(value, i) {
129 result = append(result, value)
130 }
131 }
132
133 return result
134 }
135
136 func TryFilter[T any](slice []T, test func(T) (bool, error)) ([]T, error) {
137 result := make([]T, 0, len(slice))
138 for _, element := range slice {
139 ok, err := test(element)
140 if err != nil {
141 return nil, err
142 }
143 if ok {
144 result = append(result, element)
145 }
146 }
147 return result, nil
148 }
149
150 func TryFilterWithIndex[T any](slice []T, test func(T, int) (bool, error)) ([]T, error) {
151 result := make([]T, 0, len(slice))
152 for i, element := range slice {
153 ok, err := test(element, i)
154 if err != nil {
155 return nil, err
156 }
157 if ok {
158 result = append(result, element)
159 }
160 }
161 return result, nil
162 }
163
164 // Mutates original slice. Intended usage is to reassign the slice result to the input slice.
165 func FilterInPlace[T any](slice []T, test func(T) bool) []T {
166 newLength := 0
167 for _, element := range slice {
168 if test(element) {
169 slice[newLength] = element
170 newLength++
171 }
172 }
173
174 return slice[:newLength]
175 }
176
177 // Produces a new slice, leaves the input slice untouched
178 func Reverse[T any](slice []T) []T {
179 result := make([]T, len(slice))
180 for i := range slice {
181 result[i] = slice[len(slice)-1-i]
182 }
183 return result
184 }
185
186 func ReverseInPlace[T any](slice []T) {
187 for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
188 slice[i], slice[j] = slice[j], slice[i]
189 }
190 }
191
192 // Produces a new slice, leaves the input slice untouched.
193 func FilterMap[T any, E any](slice []T, test func(T) (E, bool)) []E {
194 result := make([]E, 0, len(slice))
195 for _, element := range slice {
196 mapped, ok := test(element)
197 if ok {
198 result = append(result, mapped)
199 }
200 }
201
202 return result
203 }
204
205 func FilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool)) []E {
206 result := make([]E, 0, len(slice))
207 for i, element := range slice {
208 mapped, ok := test(element, i)
209 if ok {
210 result = append(result, mapped)
211 }
212 }
213
214 return result
215 }
216
217 func TryFilterMap[T any, E any](slice []T, test func(T) (E, bool, error)) ([]E, error) {
218 result := make([]E, 0, len(slice))
219 for _, element := range slice {
220 mapped, ok, err := test(element)
221 if err != nil {
222 return nil, err
223 }
224 if ok {
225 result = append(result, mapped)
226 }
227 }
228
229 return result, nil
230 }
231
232 func TryFilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool, error)) ([]E, error) {
233 result := make([]E, 0, len(slice))
234 for i, element := range slice {
235 mapped, ok, err := test(element, i)
236 if err != nil {
237 return nil, err
238 }
239 if ok {
240 result = append(result, mapped)
241 }
242 }
243
244 return result, nil
245 }
246
247 // Prepends items to the beginning of a slice.
248 // E.g. Prepend([]int{1,2}, 3, 4) = []int{3,4,1,2}
249 // Mutates original slice. Intended usage is to reassign the slice result to the input slice.
250 func Prepend[T any](slice []T, values ...T) []T {
251 return append(values, slice...)
252 }
253
254 // Removes the element at the given index. Intended usage is to reassign the result to the input slice.
255 func Remove[T any](slice []T, index int) []T {
256 return slices.Delete(slice, index, index+1)
257 }
258
259 // Removes the element at the 'fromIndex' and then inserts it at 'toIndex'.
260 // Operates on the input slice. Expected use is to reassign the result to the input slice.
261 func Move[T any](slice []T, fromIndex int, toIndex int) []T {
262 item := slice[fromIndex]
263 slice = Remove(slice, fromIndex)
264 return slices.Insert(slice, toIndex, item)
265 }
266
267 // Swaps two elements at the given indices.
268 // Operates on the input slice.
269 func Swap[T any](slice []T, index1 int, index2 int) {
270 slice[index1], slice[index2] = slice[index2], slice[index1]
271 }
272
273 // Similar to Append but we leave the original slice untouched and return a new slice
274 func Concat[T any](slice []T, values ...T) []T {
275 newSlice := make([]T, 0, len(slice)+len(values))
276 newSlice = append(newSlice, slice...)
277 newSlice = append(newSlice, values...)
278 return newSlice
279 }
280
281 func ContainsFunc[T any](slice []T, f func(T) bool) bool {
282 return IndexFunc(slice, f) != -1
283 }
284
285 // Pops item from the end of the slice and returns it, along with the updated slice
286 // Mutates original slice. Intended usage is to reassign the slice result to the input slice.
287 func Pop[T any](slice []T) (T, []T) {
288 index := len(slice) - 1
289 value := slice[index]
290 slice = slice[0:index]
291 return value, slice
292 }
293
294 // Shifts item from the beginning of the slice and returns it, along with the updated slice.
295 // Mutates original slice. Intended usage is to reassign the slice result to the input slice.
296 func Shift[T any](slice []T) (T, []T) {
297 value := slice[0]
298 slice = slice[1:]
299 return value, slice
300 }
301
302 func Partition[T any](slice []T, test func(T) bool) ([]T, []T) {
303 left := make([]T, 0, len(slice))
304 right := make([]T, 0, len(slice))
305
306 for _, value := range slice {
307 if test(value) {
308 left = append(left, value)
309 } else {
310 right = append(right, value)
311 }
312 }
313
314 return left, right
315 }
316
317 func MaxBy[T any, V constraints.Ordered](slice []T, f func(T) V) V {
318 if len(slice) == 0 {
319 return zero[V]()
320 }
321
322 max := f(slice[0])
323 for _, element := range slice[1:] {
324 value := f(element)
325 if value > max {
326 max = value
327 }
328 }
329 return max
330 }
331
332 func MinBy[T any, V constraints.Ordered](slice []T, f func(T) V) V {
333 if len(slice) == 0 {
334 return zero[V]()
335 }
336
337 min := f(slice[0])
338 for _, element := range slice[1:] {
339 value := f(element)
340 if value < min {
341 min = value
342 }
343 }
344 return min
345 }
346
347 func Find[T any](slice []T, f func(T) bool) (T, bool) {
348 for _, element := range slice {
349 if f(element) {
350 return element, true
351 }
352 }
353 return zero[T](), false
354 }
355
356 // Sometimes you need to find an element and then map it to some other value based on
357 // information you obtained while finding it. This function lets you do that
358 func FindMap[T any, V any](slice []T, f func(T) (V, bool)) (V, bool) {
359 for _, element := range slice {
360 if value, ok := f(element); ok {
361 return value, true
362 }
363 }
364 return zero[V](), false
365 }
366
367 func ForEach[T any](slice []T, f func(T)) {
368 for _, element := range slice {
369 f(element)
370 }
371 }
372
373 func ForEachWithIndex[T any](slice []T, f func(T, int)) {
374 for i, element := range slice {
375 f(element, i)
376 }
377 }
378
379 func TryForEach[T any](slice []T, f func(T) error) error {
380 for _, element := range slice {
381 if err := f(element); err != nil {
382 return err
383 }
384 }
385 return nil
386 }
387
388 func TryForEachWithIndex[T any](slice []T, f func(T, int) error) error {
389 for i, element := range slice {
390 if err := f(element, i); err != nil {
391 return err
392 }
393 }
394 return nil
395 }
396
397 func Sum[T constraints.Ordered](i []T) T {
398 sum := zero[T]()
399 for _, value := range i {
400 sum += value
401 }
402 return sum
403 }
404
405 func zero[T any]() T {
406 var value T
407 return value
408 }