aboutsummaryrefslogtreecommitdiff
path: root/go/utils/slices.go
blob: eb02f782c8de824795df19d75539a7bfd7823ff2 (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
package utils

func TypedSliceToInterfaceSlice[T any](s []T) []interface{} {
	is := make([]interface{}, 0, len(s))
	for _, t := range s {
		is = append(is, t)
	}
	return is
}

func InterfaceSliceToTypedSlice[T any](s []interface{}) []T {
	ts := make([]T, 0, len(s))
	for _, t := range s {
		ts = append(ts, t.(T))
	}
	return ts
}

func RemoveFromUnorderedSlice[T comparable](s []T, o T) []T {
	for i, t := range s {
		if t != o {
			continue
		}

		s[i] = s[len(s) - 1]
		return s[:len(s) - 1]
	}

	return s
}