mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
Adds capacity hints to both keep and reject slices in Partition function to prevent repeated allocations during append operations. Changes: - keep: make([]T, 0) → make([]T, 0, len(values)) - reject: make([]T, 0) → make([]T, 0, len(values)) Impact: - Reduces allocations from O(log n) to O(1) for each slice - Improves performance by eliminating slice growth overhead - Minimal memory overhead as worst case is original slice size 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
17 lines
453 B
Go
17 lines
453 B
Go
package underscore
|
|
|
|
// Partition splits the slice into two slices: one whose elements all satisfy predicate
|
|
// and one whose elements all do not satisfy predicate.
|
|
func Partition[T any](values []T, predicate func(T) bool) ([]T, []T) {
|
|
keep := make([]T, 0, len(values))
|
|
reject := make([]T, 0, len(values))
|
|
|
|
for _, v := range values {
|
|
if predicate(v) {
|
|
keep = append(keep, v)
|
|
} else {
|
|
reject = append(reject, v)
|
|
}
|
|
}
|
|
return keep, reject
|
|
}
|