Added moslice.Batch
All checks were successful
ci / deploy (push) Successful in 54s

This commit is contained in:
Leon Mika 2026-03-02 22:07:07 +11:00
parent cdaa1c8abb
commit 31c6b125c9
2 changed files with 72 additions and 0 deletions

20
moslice/batch.go Normal file
View 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
}

52
moslice/batch_test.go Normal file
View file

@ -0,0 +1,52 @@
package moslice
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBatch(t *testing.T) {
t.Run("empty slice", func(t *testing.T) {
result := Batch([]int{}, 3)
assert.Nil(t, result)
})
t.Run("zero or negative size", func(t *testing.T) {
result := Batch([]int{1, 2, 3}, 0)
assert.Nil(t, result)
result = Batch([]int{1, 2, 3}, -1)
assert.Nil(t, result)
})
t.Run("slice smaller than batch size", func(t *testing.T) {
result := Batch([]int{1, 2}, 5)
assert.Equal(t, [][]int{{1, 2}}, result)
})
t.Run("slice equal to batch size", func(t *testing.T) {
result := Batch([]int{1, 2, 3}, 3)
assert.Equal(t, [][]int{{1, 2, 3}}, result)
})
t.Run("slice evenly divisible by batch size", func(t *testing.T) {
result := Batch([]int{1, 2, 3, 4, 5, 6}, 2)
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5, 6}}, result)
})
t.Run("slice not evenly divisible by batch size", func(t *testing.T) {
result := Batch([]int{1, 2, 3, 4, 5}, 2)
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5}}, result)
})
t.Run("batch size of 1", func(t *testing.T) {
result := Batch([]int{1, 2, 3}, 1)
assert.Equal(t, [][]int{{1}, {2}, {3}}, result)
})
t.Run("string slice", func(t *testing.T) {
result := Batch([]string{"a", "b", "c", "d", "e"}, 2)
assert.Equal(t, [][]string{{"a", "b"}, {"c", "d"}, {"e"}}, result)
})
}