16e34a806e
Funciones genericas reutilizables: - RetryWithBackoff: reintento con backoff exponencial (impure) - Memoize: cache de funciones puras (pure) - Pipeline: composición T→T en secuencia (pure) - MapConcurrent: map paralelo con worker pool (impure) - Partition: divide slice en dos por predicado (pure) - Chunk: divide slice en trozos de tamaño N (pure) Todas con implementación real y documentación .md. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
26 lines
527 B
Go
26 lines
527 B
Go
package core
|
|
|
|
// Chunk splits xs into sub-slices of the given size.
|
|
// The last chunk may contain fewer than size elements.
|
|
// Returns nil if xs is empty. Panics if size <= 0.
|
|
func Chunk[T any](xs []T, size int) [][]T {
|
|
if size <= 0 {
|
|
panic("chunk size must be > 0")
|
|
}
|
|
n := len(xs)
|
|
if n == 0 {
|
|
return nil
|
|
}
|
|
|
|
numChunks := (n + size - 1) / size
|
|
chunks := make([][]T, 0, numChunks)
|
|
for i := 0; i < n; i += size {
|
|
end := i + size
|
|
if end > n {
|
|
end = n
|
|
}
|
|
chunks = append(chunks, xs[i:end])
|
|
}
|
|
return chunks
|
|
}
|