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:
@ -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]
|
||||
}
|
||||
|
Reference in New Issue
Block a user