pomerium/pkg/slices/slices.go
Caleb Doxsey 10662d7034
databroker: fix fast forward (#4192)
* databroker: sort configs

* databroker: fix fast-forward

* newest not oldest
2023-05-23 15:30:27 -06:00

68 lines
1.3 KiB
Go

// Package slices contains functions for working with slices.
package slices
// Contains returns true if e is in s.
func Contains[S ~[]E, E comparable](s S, e E) bool {
for _, el := range s {
if el == e {
return true
}
}
return false
}
// Filter returns a new slice containing only those elements for which f(element) is true.
func Filter[S ~[]E, E any](s S, f func(E) bool) S {
var ns S
for _, el := range s {
if f(el) {
ns = append(ns, el)
}
}
return ns
}
// Remove removes e from s.
func Remove[S ~[]E, E comparable](s S, e E) S {
var ns S
for _, el := range s {
if el != e {
ns = append(ns, el)
}
}
return ns
}
// Reverse reverses a slice's order.
func Reverse[S ~[]E, E comparable](s S) {
for i := 0; i < len(s)/2; i++ {
s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i]
}
}
// Unique returns the unique elements of s.
func Unique[S ~[]E, E comparable](s S) S {
var ns S
h := map[E]struct{}{}
for _, el := range s {
if _, ok := h[el]; !ok {
h[el] = struct{}{}
ns = append(ns, el)
}
}
return ns
}
// UniqueBy returns the unique elements of s using a function to map elements.
func UniqueBy[S ~[]E, E any, V comparable](s S, by func(E) V) S {
var ns S
h := map[V]struct{}{}
for _, el := range s {
v := by(el)
if _, ok := h[v]; !ok {
h[v] = struct{}{}
ns = append(ns, el)
}
}
return ns
}