diff --git a/chunk.go b/chunk.go index cd078da..c98f5c3 100644 --- a/chunk.go +++ b/chunk.go @@ -3,10 +3,6 @@ package hoff // Chunk takes an input array of T elements, and "chunks" into groups of chunkSize elements. // If the input array is empty, or the batchSize is 0, return an empty slice of slices. // Adapted to generics from https://github.com/golang/go/wiki/SliceTricks#batching-with-minimal-allocation. -// Examples: -// Chunk([]int{1, 2, 3, 4, 5}, 2) = [[1, 2] [3, 4], [5]] -// Chunk([]int{}, 2) = [] -// Chunk([]int{1, 2, 3}, 0) = []. func Chunk[T any](actions []T, batchSize int) [][]T { // if the input is empty or batch size is 0, return an empty slice of slices if len(actions) == 0 || batchSize < 1 { diff --git a/chunk_test.go b/chunk_test.go index d2e490b..15a648a 100644 --- a/chunk_test.go +++ b/chunk_test.go @@ -1,6 +1,7 @@ package hoff import ( + "fmt" "testing" "github.com/stretchr/testify/require" @@ -114,3 +115,18 @@ func TestChunkStructs(t *testing.T) { require.Equal(t, testCase.ExpectedOutput, output, testCase.Message) } } + +func ExampleChunk() { + fmt.Println(Chunk([]int{1, 2, 3, 4, 5}, 2)) + // Output: [[1 2] [3 4] [5]] +} + +func ExampleChunk_withEmptyArray() { + fmt.Println(Chunk([]int{}, 2)) + // Output: [] +} + +func ExampleChunk_zeroBatchSize() { + fmt.Println(Chunk([]int{1, 2, 3}, 0)) + // Output: [] +}