Refactor Strip (already).

Stripping cards from the Hand will also be useful, so I pulled the logic of Strip out into arrayutil (more efficiently, too) and rewrote deck.Strip to use it.
This commit is contained in:
2023-04-01 19:40:28 -07:00
parent 222d4375ee
commit 75de281cee
2 changed files with 22 additions and 18 deletions

View File

@ -126,3 +126,22 @@ func ShufflePart[T any](slice []T, r *rand.Rand, loc, n int) {
}
ShuffleAll(slice[loc:loc+n], r)
}
// Strip iterates T, removing any element for which removeWhen returns true
// (when provided the index of the element and the element itself as arguments).
// It returns the stripped slice.
func Strip[T any](slice []T, removeWhen func(idx int, t T) bool) []T {
if len(slice) == 0 {
return nil
}
to := 0
for from, e := range slice {
if !removeWhen(from, e) {
if to != from {
slice[to] = slice[from]
}
to++
}
}
return slice[:to]
}