util/slicesx: add FirstElementEqual and LastElementEqual
And update a few callers as examples of motivation. (there are a couple others, but these are the ones where it's prettier) Updates #cleanup Change-Id: Ic8c5cb7af0a59c6e790a599136b591ebe16d38eb Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:

committed by
Brad Fitzpatrick

parent
910462a8e0
commit
cec779e771
@ -136,3 +136,15 @@ func CutSuffix[E comparable](s, suffix []E) (after []E, found bool) {
|
||||
}
|
||||
return s[:len(s)-len(suffix)], true
|
||||
}
|
||||
|
||||
// FirstEqual reports whether len(s) > 0 and
|
||||
// its first element == v.
|
||||
func FirstEqual[T comparable](s []T, v T) bool {
|
||||
return len(s) > 0 && s[0] == v
|
||||
}
|
||||
|
||||
// LastEqual reports whether len(s) > 0 and
|
||||
// its last element == v.
|
||||
func LastEqual[T comparable](s []T, v T) bool {
|
||||
return len(s) > 0 && s[len(s)-1] == v
|
||||
}
|
||||
|
Reference in New Issue
Block a user