device: add generic methods for working with user+session devices (#3710)

This commit is contained in:
Caleb Doxsey 2022-10-28 08:41:12 -06:00 committed by GitHub
parent 6a9d6e45e1
commit 3f9dfbef76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 36 deletions

47
pkg/slices/slices.go Normal file
View file

@ -0,0 +1,47 @@
// 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
}
// 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
}