This commit is contained in:
parent
cdaa1c8abb
commit
31c6b125c9
2 changed files with 72 additions and 0 deletions
20
moslice/batch.go
Normal file
20
moslice/batch.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue