Skip to content

Commit c22a32c

Browse files
author
Mr Kiwi
committed
Add chunk function with test cases
Implemented a utility function `Chunk` to split slices into chunks of specified sizes. Added comprehensive test cases for different slice and chunk size scenarios. Updated dependencies to include testing libraries such as `testify`.
1 parent 66689b4 commit c22a32c

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/pkg/errors v0.9.1
1818
github.com/sanity-io/litter v1.5.5
1919
github.com/spf13/cobra v1.8.1
20+
github.com/stretchr/testify v1.9.0
2021
github.com/surrealdb/surrealdb.go v0.2.1
2122
go.uber.org/atomic v1.11.0
2223
go.uber.org/fx v1.22.2
@@ -25,6 +26,7 @@ require (
2526
)
2627

2728
require (
29+
github.com/davecgh/go-spew v1.1.1 // indirect
2830
github.com/dustin/go-humanize v1.0.1 // indirect
2931
github.com/go-ini/ini v1.67.0 // indirect
3032
github.com/goccy/go-json v0.10.3 // indirect
@@ -33,11 +35,13 @@ require (
3335
github.com/klauspost/compress v1.17.10 // indirect
3436
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
3537
github.com/minio/md5-simd v1.1.2 // indirect
38+
github.com/pmezard/go-difflib v1.0.0 // indirect
3639
github.com/rs/xid v1.6.0 // indirect
3740
github.com/spf13/pflag v1.0.5 // indirect
3841
go.uber.org/dig v1.18.0 // indirect
3942
golang.org/x/crypto v0.27.0 // indirect
4043
golang.org/x/net v0.29.0 // indirect
4144
golang.org/x/sys v0.25.0 // indirect
4245
golang.org/x/text v0.18.0 // indirect
46+
gopkg.in/yaml.v3 v3.0.1 // indirect
4347
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
8787
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
8888
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
8989
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
90+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
9091
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9192
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9293
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

slices/chunk.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package slices
2+
3+
// Chunk divides a slice of elements into multiple smaller slices, each of a specified size.
4+
//
5+
// The function takes a generic type parameter `T` which allows it to work with slices of any type.
6+
//
7+
// Parameters:
8+
// - elements: A slice of elements of type `T` to be divided into chunks.
9+
// - chunkSize: An integer representing the size of each chunk. If chunkSize is less than or equal to 0,
10+
// an empty slice is returned.
11+
//
12+
// Returns:
13+
// - A two-dimensional slice of type `T` where each inner slice is of length `chunkSize` (except possibly the last one).
14+
// If chunkSize is less than or equal to 0, an empty slice of slices is returned. If the input slice is empty,
15+
// an empty slice of slices is returned.
16+
//
17+
// Example usage:
18+
//
19+
// chunks := Chunk([]int{1, 2, 3, 4, 5, 6}, 2)
20+
// // chunks will be [][]int{{1, 2}, {3, 4}, {5, 6}}
21+
//
22+
// chunks = Chunk([]int{1, 2, 3, 4, 5, 6}, 3)
23+
// // chunks will be [][]int{{1, 2, 3}, {4, 5, 6}}
24+
//
25+
// chunks = Chunk([]int{1, 2, 3, 4, 5, 6}, 0)
26+
// // chunks will be [][]int{}
27+
//
28+
// chunks = Chunk([]int{}, 3)
29+
// // chunks will be [][]int{}
30+
func Chunk[T any](elements []T, chunkSize int) [][]T {
31+
if chunkSize <= 0 {
32+
return [][]T{}
33+
}
34+
35+
capacity := (len(elements) + chunkSize - 1) / chunkSize
36+
chunks := make([][]T, capacity)
37+
38+
for i, chunkIndex := 0, 0; i < len(elements); i += chunkSize {
39+
end := i + chunkSize
40+
isEndExceeding := end > len(elements)
41+
if isEndExceeding {
42+
end = len(elements)
43+
}
44+
chunks[chunkIndex] = elements[i:end]
45+
chunkIndex++
46+
}
47+
return chunks
48+
}

slices/chunk_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package slices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestChunk(t *testing.T) {
10+
type testCase struct {
11+
input []int
12+
chunkSize int
13+
expected [][]int
14+
}
15+
16+
testCases := []testCase{
17+
{
18+
input: []int{1, 2, 3, 4, 5, 6},
19+
chunkSize: 2,
20+
expected: [][]int{{1, 2}, {3, 4}, {5, 6}},
21+
},
22+
{
23+
input: []int{1, 2, 3, 4, 5, 6},
24+
chunkSize: 3,
25+
expected: [][]int{{1, 2, 3}, {4, 5, 6}},
26+
},
27+
{
28+
input: []int{1, 2},
29+
chunkSize: 5,
30+
expected: [][]int{{1, 2}},
31+
},
32+
{
33+
input: []int{},
34+
chunkSize: 3,
35+
expected: [][]int{},
36+
},
37+
{
38+
input: []int{1, 2, 3, 4, 5, 6},
39+
chunkSize: 0,
40+
expected: [][]int{},
41+
},
42+
}
43+
44+
r := require.New(t)
45+
for _, tc := range testCases {
46+
result := Chunk(tc.input, tc.chunkSize)
47+
r.Equal(tc.expected, result, "Chunk(%v, %d)", tc.input, tc.chunkSize)
48+
}
49+
}

0 commit comments

Comments
 (0)