1package u
2
3func Filter[T any](v []T, f func(T) bool) []T {
4 var result []T
5
6 for _, s := range v {
7 if f(s) {
8 result = append(result, s)
9 }
10 }
11
12 return result
13}
14
15func First[T any](v []T) (T, bool) {
16 if len(v) == 0 {
17 var zero T
18 return zero, false
19 }
20 return v[0], true
21}
22
23func FirstOrZero[T any](v []T) T {
24 if len(v) == 0 {
25 var zero T
26 return zero
27 }
28 return v[0]
29}
30
31func LastOrZero[T any](v []T) T {
32 if len(v) == 0 {
33 var zero T
34 return zero
35 }
36 return v[len(v)-1]
37}
38
39func ChunkBy[T any](items []T, chunkSize int) [][]T {
40 chunks := make([][]T, 0, (len(items)/chunkSize)+1)
41 for chunkSize < len(items) {
42 items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
43 }
44 return append(chunks, items)
45}