yoake/internal/util/slice.go

50 lines
920 B
Go
Raw Permalink Normal View History

2022-11-13 00:17:42 -06:00
package util
// Contain checks if an element is in a slice.
func Contain[T comparable](a []T, x T) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
// Unique returns a slice with all duplicate elements removed.
func Unique[T comparable](a []T) []T {
var result []T
for _, e := range a {
if !Contain(result, e) {
result = append(result, e)
}
}
return result
}
// AntiJOIN returns a slice with all elements in a that are not in b.
func AntiJoin[T comparable](a []T, b []T) []T {
var result []T
for _, e := range a {
if !Contain(b, e) {
result = append(result, e)
}
}
return result
}
2022-11-16 14:54:46 -06:00
2022-12-07 09:47:37 -06:00
func Join[T any](vals ...[]T) []T {
result := make([]T, 0)
for _, val := range vals {
result = append(result, val...)
}
return result
}
2022-11-16 14:54:46 -06:00
func Reverse[T any](a []T) []T {
var result []T
for i := len(a) - 1; i >= 0; i-- {
result = append(result, a[i])
}
return result
}