mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-04 01:09:36 +02:00
device: add generic methods for working with user+session devices (#3710)
This commit is contained in:
parent
6a9d6e45e1
commit
3f9dfbef76
5 changed files with 75 additions and 36 deletions
47
pkg/slices/slices.go
Normal file
47
pkg/slices/slices.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue