-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.go
79 lines (68 loc) · 1.75 KB
/
slice.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
package slice
import (
"sort"
"github.com/kamstrup/fn/constraints"
)
// Mapping converts a slice from one type to another
func Mapping[S, T any](slice []S, f func(s S) T) []T {
if len(slice) == 0 {
return []T{}
}
result := make([]T, len(slice))
for i, s := range slice {
result[i] = f(s)
}
return result
}
// MappingIndex converts a slice from one type to another.
// The mapping function also receives the index of the element being transformed.
func MappingIndex[S, T any](slice []S, f func(i int, s S) T) []T {
if len(slice) == 0 {
return []T{}
}
result := make([]T, len(slice))
for i, s := range slice {
result[i] = f(i, s)
}
return result
}
// Gen builds a new slice of a given size.
// The generator function is called for each index in the new slice.
func Gen[T any](sz int, generator func(i int) T) []T {
result := make([]T, sz)
for i := range result {
result[i] = generator(i)
}
return result
}
// Copy returns a shallow copy of the given slice.
func Copy[T any](slice []T) []T {
cpy := make([]T, len(slice))
copy(cpy, slice)
return cpy
}
// Zero zeroes all elements in a slice.
func Zero[T any](slice []T) []T {
// compiles to runtime.memclr(), see https://github.com/golang/go/issues/5373
var zero T
for i := range slice {
slice[i] = zero
}
return slice
}
// SortAsc sorts a slice in-place.
// The argument is returned to facilitate easy chaining.
func SortAsc[T constraints.Ordered](slice []T) []T {
sort.Slice(slice, func(i, j int) bool {
return slice[i] < slice[j]
})
return slice
}
// SortDesc sorts a slice in-place.
// The argument is returned to facilitate easy chaining.
func SortDesc[T constraints.Ordered](slice []T) []T {
sort.Slice(slice, func(i, j int) bool {
return slice[i] > slice[j]
})
return slice
}