File: slices.go

package info (click to toggle)
golang-github-jesseduffield-generics 0.0~git20250406.4f541cb-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 196 kB
  • sloc: makefile: 2
file content (408 lines) | stat: -rw-r--r-- 9,461 bytes parent folder | download
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package slices

import (
	"golang.org/x/exp/constraints"
	"golang.org/x/exp/slices"
)

// This file contains the new functions that do not live in the official slices package.

func Some[T any](slice []T, test func(T) bool) bool {
	for _, value := range slice {
		if test(value) {
			return true
		}
	}

	return false
}

func Every[T any](slice []T, test func(T) bool) bool {
	for _, value := range slice {
		if !test(value) {
			return false
		}
	}

	return true
}

// Produces a new slice, leaves the input slice untouched.
func Map[T any, V any](slice []T, f func(T) V) []V {
	result := make([]V, 0, len(slice))
	for _, value := range slice {
		result = append(result, f(value))
	}

	return result
}

// Produces a new slice, leaves the input slice untouched.
func MapWithIndex[T any, V any](slice []T, f func(T, int) V) []V {
	result := make([]V, 0, len(slice))
	for i, value := range slice {
		result = append(result, f(value, i))
	}

	return result
}

func TryMap[T any, V any](slice []T, f func(T) (V, error)) ([]V, error) {
	result := make([]V, 0, len(slice))
	for _, value := range slice {
		output, err := f(value)
		if err != nil {
			return nil, err
		}
		result = append(result, output)
	}

	return result, nil
}

func TryMapWithIndex[T any, V any](slice []T, f func(T, int) (V, error)) ([]V, error) {
	result := make([]V, 0, len(slice))
	for i, value := range slice {
		output, err := f(value, i)
		if err != nil {
			return nil, err
		}
		result = append(result, output)
	}

	return result, nil
}

// Produces a new slice, leaves the input slice untouched.
func FlatMap[T any, V any](slice []T, f func(T) []V) []V {
	// impossible to know how long this slice will be in the end but the length
	// of the original slice is the lower bound
	result := make([]V, 0, len(slice))
	for _, value := range slice {
		result = append(result, f(value)...)
	}

	return result
}

func FlatMapWithIndex[T any, V any](slice []T, f func(T, int) []V) []V {
	// impossible to know how long this slice will be in the end but the length
	// of the original slice is the lower bound
	result := make([]V, 0, len(slice))
	for i, value := range slice {
		result = append(result, f(value, i)...)
	}

	return result
}

func Flatten[T any](slice [][]T) []T {
	result := make([]T, 0, len(slice))
	for _, subSlice := range slice {
		result = append(result, subSlice...)
	}
	return result
}

func MapInPlace[T any](slice []T, f func(T) T) {
	for i, value := range slice {
		slice[i] = f(value)
	}
}

// Produces a new slice, leaves the input slice untouched.
func Filter[T any](slice []T, test func(T) bool) []T {
	result := make([]T, 0)
	for _, element := range slice {
		if test(element) {
			result = append(result, element)
		}
	}
	return result
}

// Produces a new slice, leaves the input slice untouched.
func FilterWithIndex[T any](slice []T, f func(T, int) bool) []T {
	result := make([]T, 0, len(slice))
	for i, value := range slice {
		if f(value, i) {
			result = append(result, value)
		}
	}

	return result
}

func TryFilter[T any](slice []T, test func(T) (bool, error)) ([]T, error) {
	result := make([]T, 0)
	for _, element := range slice {
		ok, err := test(element)
		if err != nil {
			return nil, err
		}
		if ok {
			result = append(result, element)
		}
	}
	return result, nil
}

func TryFilterWithIndex[T any](slice []T, test func(T, int) (bool, error)) ([]T, error) {
	result := make([]T, 0)
	for i, element := range slice {
		ok, err := test(element, i)
		if err != nil {
			return nil, err
		}
		if ok {
			result = append(result, element)
		}
	}
	return result, nil
}

// Mutates original slice. Intended usage is to reassign the slice result to the input slice.
func FilterInPlace[T any](slice []T, test func(T) bool) []T {
	newLength := 0
	for _, element := range slice {
		if test(element) {
			slice[newLength] = element
			newLength++
		}
	}

	return slice[:newLength]
}

// Produces a new slice, leaves the input slice untouched
func Reverse[T any](slice []T) []T {
	result := make([]T, len(slice))
	for i := range slice {
		result[i] = slice[len(slice)-1-i]
	}
	return result
}

func ReverseInPlace[T any](slice []T) {
	for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
		slice[i], slice[j] = slice[j], slice[i]
	}
}

// Produces a new slice, leaves the input slice untouched.
func FilterMap[T any, E any](slice []T, test func(T) (E, bool)) []E {
	result := make([]E, 0, len(slice))
	for _, element := range slice {
		mapped, ok := test(element)
		if ok {
			result = append(result, mapped)
		}
	}

	return result
}

func FilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool)) []E {
	result := make([]E, 0, len(slice))
	for i, element := range slice {
		mapped, ok := test(element, i)
		if ok {
			result = append(result, mapped)
		}
	}

	return result
}

func TryFilterMap[T any, E any](slice []T, test func(T) (E, bool, error)) ([]E, error) {
	result := make([]E, 0, len(slice))
	for _, element := range slice {
		mapped, ok, err := test(element)
		if err != nil {
			return nil, err
		}
		if ok {
			result = append(result, mapped)
		}
	}

	return result, nil
}

func TryFilterMapWithIndex[T any, E any](slice []T, test func(T, int) (E, bool, error)) ([]E, error) {
	result := make([]E, 0, len(slice))
	for i, element := range slice {
		mapped, ok, err := test(element, i)
		if err != nil {
			return nil, err
		}
		if ok {
			result = append(result, mapped)
		}
	}

	return result, nil
}

// Prepends items to the beginning of a slice.
// E.g. Prepend([]int{1,2}, 3, 4) = []int{3,4,1,2}
// Mutates original slice. Intended usage is to reassign the slice result to the input slice.
func Prepend[T any](slice []T, values ...T) []T {
	return append(values, slice...)
}

// Removes the element at the given index. Intended usage is to reassign the result to the input slice.
func Remove[T any](slice []T, index int) []T {
	return slices.Delete(slice, index, index+1)
}

// Removes the element at the 'fromIndex' and then inserts it at 'toIndex'.
// Operates on the input slice. Expected use is to reassign the result to the input slice.
func Move[T any](slice []T, fromIndex int, toIndex int) []T {
	item := slice[fromIndex]
	slice = Remove(slice, fromIndex)
	return slices.Insert(slice, toIndex, item)
}

// Swaps two elements at the given indices.
// Operates on the input slice.
func Swap[T any](slice []T, index1 int, index2 int) {
	slice[index1], slice[index2] = slice[index2], slice[index1]
}

// Similar to Append but we leave the original slice untouched and return a new slice
func Concat[T any](slice []T, values ...T) []T {
	newSlice := make([]T, 0, len(slice)+len(values))
	newSlice = append(newSlice, slice...)
	newSlice = append(newSlice, values...)
	return newSlice
}

func ContainsFunc[T any](slice []T, f func(T) bool) bool {
	return IndexFunc(slice, f) != -1
}

// Pops item from the end of the slice and returns it, along with the updated slice
// Mutates original slice. Intended usage is to reassign the slice result to the input slice.
func Pop[T any](slice []T) (T, []T) {
	index := len(slice) - 1
	value := slice[index]
	slice = slice[0:index]
	return value, slice
}

// Shifts item from the beginning of the slice and returns it, along with the updated slice.
// Mutates original slice. Intended usage is to reassign the slice result to the input slice.
func Shift[T any](slice []T) (T, []T) {
	value := slice[0]
	slice = slice[1:]
	return value, slice
}

func Partition[T any](slice []T, test func(T) bool) ([]T, []T) {
	left := make([]T, 0, len(slice))
	right := make([]T, 0, len(slice))

	for _, value := range slice {
		if test(value) {
			left = append(left, value)
		} else {
			right = append(right, value)
		}
	}

	return left, right
}

func MaxBy[T any, V constraints.Ordered](slice []T, f func(T) V) V {
	if len(slice) == 0 {
		return zero[V]()
	}

	max := f(slice[0])
	for _, element := range slice[1:] {
		value := f(element)
		if value > max {
			max = value
		}
	}
	return max
}

func MinBy[T any, V constraints.Ordered](slice []T, f func(T) V) V {
	if len(slice) == 0 {
		return zero[V]()
	}

	min := f(slice[0])
	for _, element := range slice[1:] {
		value := f(element)
		if value < min {
			min = value
		}
	}
	return min
}

func Find[T any](slice []T, f func(T) bool) (T, bool) {
	for _, element := range slice {
		if f(element) {
			return element, true
		}
	}
	return zero[T](), false
}

// Sometimes you need to find an element and then map it to some other value based on
// information you obtained while finding it. This function lets you do that
func FindMap[T any, V any](slice []T, f func(T) (V, bool)) (V, bool) {
	for _, element := range slice {
		if value, ok := f(element); ok {
			return value, true
		}
	}
	return zero[V](), false
}

func ForEach[T any](slice []T, f func(T)) {
	for _, element := range slice {
		f(element)
	}
}

func ForEachWithIndex[T any](slice []T, f func(T, int)) {
	for i, element := range slice {
		f(element, i)
	}
}

func TryForEach[T any](slice []T, f func(T) error) error {
	for _, element := range slice {
		if err := f(element); err != nil {
			return err
		}
	}
	return nil
}

func TryForEachWithIndex[T any](slice []T, f func(T, int) error) error {
	for i, element := range slice {
		if err := f(element, i); err != nil {
			return err
		}
	}
	return nil
}

func Sum[T constraints.Ordered](i []T) T {
	sum := zero[T]()
	for _, value := range i {
		sum += value
	}
	return sum
}

func zero[T any]() T {
	var value T
	return value
}