mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-10 00:51:07 +00:00
util/slicesx: add Partition function
Signed-off-by: Andrew Dunham <andrew@du.nham.ca> Change-Id: If97995ca9ee9fad40f327420dcb1857dd7ea2315
This commit is contained in:
@@ -42,3 +42,18 @@ func Shuffle[S ~[]T, T any](s S) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
}
|
||||
|
||||
// Partition returns two slices, the first containing the elements of the input
|
||||
// slice for which the callback evaluates to true, the second containing the rest.
|
||||
//
|
||||
// This function does not mutate s.
|
||||
func Partition[S ~[]T, T any](s S, cb func(T) bool) (trues, falses S) {
|
||||
for _, elem := range s {
|
||||
if cb(elem) {
|
||||
trues = append(trues, elem)
|
||||
} else {
|
||||
falses = append(falses, elem)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user