mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
- 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`.
19 lines
413 B
Go
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
|
|
}
|