modash/moslice/batch.go
Leon Mika 31c6b125c9
All checks were successful
ci / deploy (push) Successful in 54s
Added moslice.Batch
2026-03-02 22:07:07 +11:00

21 lines
352 B
Go

package moslice
func Batch[T any](ts []T, size int) [][]T {
if len(ts) == 0 || size <= 0 {
return nil
}
numBatches := (len(ts) + size - 1) / size
batches := make([][]T, 0, numBatches)
for i := 0; i < len(ts); i += size {
end := i + size
if end > len(ts) {
end = len(ts)
}
batches = append(batches, ts[i:end])
}
return batches
}