underscore/chunk.go
Ruidy 1031038d42
feat: add Chunk, ContainsBy, UniqueBy, ParallelMap, map helpers
- Add `Chunk` to split slices into groups of size n.
- Add `ContainsBy` for predicate-based containment checks.
- Add `UniqueBy` to deduplicate slices by key selector.
- Add `ParallelMap` for concurrent mapping with context and error
handling.
- Add `maps.Keys` and `maps.Values` helpers for extracting map
keys/values.
- Update README and docs for new features.
- Refactor `Contains` to use `slices.Contains`.
2025-09-01 18:03:38 -04:00

19 lines
413 B
Go

package underscore
// Chunk splits the input slice into groups of size n.
// If n <= 0, it returns nil. The final chunk may be smaller than n.
func Chunk[T any](values []T, n int) [][]T {
if n <= 0 {
return nil
}
l := len(values)
if l == 0 {
return [][]T{}
}
chunks := make([][]T, 0, (l+n-1)/n)
for i := 0; i < l; i += n {
j := min(i+n, l)
chunks = append(chunks, values[i:j])
}
return chunks
}