-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.go
More file actions
81 lines (69 loc) · 1.84 KB
/
array.go
File metadata and controls
81 lines (69 loc) · 1.84 KB
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
package fun
import (
"cmp"
"fmt"
"slices"
"github.com/rprtr258/fun/exp/zun"
)
// Copy slice
func Copy[T any](slice ...T) []T {
res := make([]T, 0, len(slice))
copy(res, slice)
return res
}
// ReverseInplace reverses slice in place.
func ReverseInplace[A any](xs []A) {
zun.Reverse(xs)
}
// Subslice returns slice from start to end without panicking on out of bounds
func Subslice[T any](start, end int, slice ...T) []T {
return zun.Subslice(start, end, slice...)
}
// Chunk divides slice into chunks of size chunkSize
func Chunk[T any](chunkSize int, slice ...T) [][]T {
if chunkSize <= 0 {
panic(fmt.Errorf("invalid chunkSize: %d", chunkSize))
}
res := make([][]T, 0, len(slice)/chunkSize+1)
for i := 0; i < len(slice); i += chunkSize {
res = append(res, Subslice(i, i+chunkSize, slice...))
}
return res
}
// ConcatMap is like Map but concatenates results
func ConcatMap[T, R any](f func(T) []R, slice ...T) []R {
res := []R{}
for _, elem := range slice {
res = append(res, f(elem)...)
}
return res
}
// All returns true if all elements satisfy the condition
func All[T any](condition func(T) bool, slice ...T) bool {
_, _, ok := zun.Index(func(elem T) bool {
return !condition(elem)
}, slice...)
return !ok
}
// Any returns true if any element satisfies the condition
func Any[T any](condition func(T) bool, slice ...T) bool {
_, _, ok := zun.Index(func(elem T) bool {
return condition(elem)
}, slice...)
return ok
}
// SortBy sorts slice in place by given function
func SortBy[T any, R cmp.Ordered](by func(T) R, slice ...T) {
slices.SortFunc(slice, func(i, j T) int {
return cmp.Compare(by(i), by(j))
})
}
// GroupBy groups elements by key
func GroupBy[T any, K comparable](by func(T) K, slice ...T) map[K][]T {
res := map[K][]T{}
for _, elem := range slice {
k := by(elem)
res[k] = append(res[k], elem)
}
return res
}