1package list
2
3func Map[V any, T any](source []V, fun func(V) T) []T {
4 result := make([]T, 0, len(source))
5 for _, s := range source {
6 result = append(result, fun(s))
7 }
8 return result
9}
10
11type Pair[T, U any] struct {
12 Left T
13 Right U
14}
15
16func Distribuite[T any](slice []T, size int) [][]T {
17 chuncks := make([][]T, size)
18
19 for i := 0; i < len(slice); i += size {
20 for x := 0; x < size; x++ {
21 end := i + x
22
23 if end >= len(slice) {
24 return chuncks
25 }
26
27 chuncks[x] = append(chuncks[x], slice[end])
28 }
29 }
30
31 return chuncks
32}
33
34func Chunck[T any](slice []T, size int) [][]T {
35 var divided [][]T
36
37 chunkSize := (len(slice) + size - 1) / size
38
39 for i := 0; i < len(slice); i += chunkSize {
40 end := i + chunkSize
41
42 if end > len(slice) {
43 end = len(slice)
44 }
45
46 divided = append(divided, slice[i:end])
47 }
48
49 return divided
50}
51
52func Zip[T, U any](left []T, right []U) []Pair[T, U] {
53 // pick the array with the smaller length
54 l := len(left)
55 if len(left) > len(right) {
56 l = len(right)
57 }
58
59 pairs := make([]Pair[T, U], len(left))
60 for i := 0; i < l; i++ {
61 pairs[i] = Pair[T, U]{left[i], right[i]}
62 }
63 return pairs
64}
65
66func Revert[T any](s []T) {
67 for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
68 s[i], s[j] = s[j], s[i]
69 }
70}