generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
slices.go
70 lines (63 loc) · 1.53 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
package slices
func Map[T, U any](slice []T, fn func(T) U) []U {
result := make([]U, len(slice))
for i, v := range slice {
result[i] = fn(v)
}
return result
}
func MapErr[T, U any](slice []T, fn func(T) (U, error)) ([]U, error) {
result := make([]U, len(slice))
for i, v := range slice {
var err error
result[i], err = fn(v)
if err != nil {
return nil, err
}
}
return result, nil
}
func Filter[T any](slice []T, fn func(T) bool) []T {
result := make([]T, 0, len(slice))
for _, v := range slice {
if fn(v) {
result = append(result, v)
}
}
return result
}
// GroupBy groups the elements of a slice by the result of a function.
func GroupBy[T any, K comparable](slice []T, fn func(T) K) map[K][]T {
result := make(map[K][]T)
for _, v := range slice {
key := fn(v)
result[key] = append(result[key], v)
}
return result
}
func Reduce[T, U any](slice []T, initial U, fn func(U, T) U) U {
result := initial
for _, v := range slice {
result = fn(result, v)
}
return result
}
// AppendOrReplace appends a value to a slice if the slice does not contain a
// value for which the given function returns true. If the slice does contain
// such a value, it is replaced.
func AppendOrReplace[T any](slice []T, value T, fn func(T) bool) []T {
for i, v := range slice {
if fn(v) {
slice[i] = value
return slice
}
}
return append(slice, value)
}
func FlatMap[T, U any](slice []T, fn func(T) []U) []U {
result := make([]U, 0, len(slice))
for _, v := range slice {
result = append(result, fn(v)...)
}
return result
}