Skip to content

Commit b40d18b

Browse files
committed
add chunk
1 parent 74b43d3 commit b40d18b

File tree

7 files changed

+47
-11
lines changed

7 files changed

+47
-11
lines changed

docs/sliceutils/chunk.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Chunk
2+
3+
`func Chunk[T any](input []T, size int) [][]T `
4+
5+
Unique takes a slice of type T and size N and returns a slice of slices T of size N.
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
13+
"github.com/Goldziher/go-utils/sliceutils"
14+
)
15+
16+
func main() {
17+
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
18+
19+
result1 := sliceutils.Chunk(numbers, 2) // [][]int{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}
20+
result2 := sliceutils.Chunk(numbers, 3) // [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}
21+
}
22+
```

docs/sliceutils/unique.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
`func Unique[T comparable](slice []T) []T`
44

5-
Unique tales a slice of type T and returns a slice of type T containing all unique elements.
5+
Unique takes a slice of type T and returns a slice of type T containing all unique elements.
66

77
```go
88
package main

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/stretchr/testify v1.7.1
7-
golang.org/x/exp v0.0.0-20220428152302-39d4317da171
7+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
88
)
99

1010
require (

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
77
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
88
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 h1:TfdoLivD44QwvssI9Sv1xwa5DcL5XQr4au4sZ2F2NV4=
99
golang.org/x/exp v0.0.0-20220428152302-39d4317da171/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
10+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
11+
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
1012
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1113
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1214
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

predicates/predicates.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

sliceutils/sliceutils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,17 @@ func Unique[T comparable](slice []T) []T {
308308
}
309309
return unique
310310
}
311+
312+
// Chunk - receives a slice of type T and size N and returns a slice of slices T of size N.
313+
func Chunk[T any](input []T, size int) [][]T {
314+
var chunks [][]T
315+
316+
for i := 0; i < len(input); i += size {
317+
end := i + size
318+
if end > len(input) {
319+
end = len(input)
320+
}
321+
chunks = append(chunks, input[i:end])
322+
}
323+
return chunks
324+
}

sliceutils/sliceutils_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,10 @@ func TestUnique(t *testing.T) {
235235
// Ensure original is unaltered
236236
assert.NotEqual(t, expectedResult, duplicates)
237237
}
238+
239+
func TestChunk(t *testing.T) {
240+
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
241+
242+
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}}, sliceutils.Chunk(numbers, 2))
243+
assert.Equal(t, [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}, sliceutils.Chunk(numbers, 3))
244+
}

0 commit comments

Comments
 (0)