underscore/partition.go
Ruidy 46d52e3cfa
perf: pre-allocate Partition result slices
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>
2025-11-14 13:48:28 +01:00

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
}