aboutsummaryrefslogtreecommitdiff
path: root/pkg/list/list.go
blob: dfc3fb7bce58752364d08c552a45be6eb47b047a (plain)
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
package list

func Map[V any, T any](source []V, fun func(V) T) []T {
	result := make([]T, 0, len(source))
	for _, s := range source {
		result = append(result, fun(s))
	}
	return result
}

type Pair[T, U any] struct {
	Left  T
	Right U
}

func Zip[T, U any](left []T, right []U) []Pair[T, U] {
	// pick the array with the smaller length
	l := len(left)
	if len(left) > len(right) {
		l = len(right)
	}

	pairs := make([]Pair[T, U], len(left))
	for i := 0; i < l; i++ {
		pairs[i] = Pair[T, U]{left[i], right[i]}
	}
	return pairs
}

func Revert[T any](s []T) {
	for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
		s[i], s[j] = s[j], s[i]
	}
}