util/slicesx: add AppendMatching

We had this in a different repo, but moving it here, as this a more
fitting package.

Updates #cleanup

Change-Id: I5fb9b10e465932aeef5841c67deba4d77d473d57
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2024-04-30 16:36:41 -07:00
committed by Brad Fitzpatrick
parent fe009c134e
commit 7455e027e9
2 changed files with 25 additions and 0 deletions

View File

@@ -91,3 +91,13 @@ func Filter[S ~[]T, T any](dst, src S, fn func(T) bool) S {
}
return dst
}
// AppendMatching appends elements in ps to dst if f(x) is true.
func AppendMatching[T any](dst, ps []T, f func(T) bool) []T {
for _, p := range ps {
if f(p) {
dst = append(dst, p)
}
}
return dst
}