Skip to content

Commit

Permalink
#16. Add ArrayChunk - split an array into chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 17, 2019
1 parent 50076ef commit 7f191b2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ pgo.InArray("bar33", []string{"foo", "bar", "baz"}) // false
pgo.InArray(3.14159, []float64{33.12, 12.333, 3.14159, 78.4429}) // true
```

Split an array by chunks (with auto-tailing)
```go
pgo.ArrayChunk([]int{1, 2, 3, 4, 5, 6, 7, 8}, 2) // [][]int{[]int{1, 2}, []int{3, 4}, []int{5, 6}, []int{7, 8}}

pgo.ArrayChunk([]string{"foo", "bar", "baz", "fizz", "buzz"}, 3) // [][]string{[]string{"foo", "bar", "baz"}, []string{"fizz", "buzz"}}
```

See more examples in *_test.go files.
20 changes: 20 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ func search(needle interface{}, haystack interface{}) bool {

return false
}

// ArrayChunk split an array into chunks
func ArrayChunk(array interface{}, size int) []interface{} {
var chunks []interface{}

s := reflect.ValueOf(array)
len := s.Len()

var subChunk []interface{}
for i := 0; i < len; i++ {
subChunk = append(subChunk, s.Index(i).Interface())

if (i+1)%size == 0 || i+1 == len {
chunks = append(chunks, subChunk)
subChunk = make([]interface{}, 0)
}
}

return chunks
}
36 changes: 34 additions & 2 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package pgo_test

import (
"pgo"
"reflect"
"testing"
)

var testInts = []struct {
var testInArray = []struct {
val interface{}
slice interface{}
result bool
Expand All @@ -19,9 +20,40 @@ var testInts = []struct {
}

func TestInArray(t *testing.T) {
for _, object := range testInts {
for _, object := range testInArray {
if pgo.InArray(object.val, object.slice) != object.result {
t.Fatalf("Want: %v, got: %v", object.result, pgo.InArray(object.val, object.slice))
}
}
}

var testArrayChunk = []struct {
array interface{}
size int
result interface{}
}{
{[]int{1, 2, 3, 4, 5, 6, 7, 8}, 2, [][]int{[]int{1, 2}, []int{3, 4}, []int{5, 6}, []int{7, 8}}},
{[]string{"foo", "bar", "baz", "fizz", "buzz"}, 3, [][]string{[]string{"foo", "bar", "baz"}, []string{"fizz", "buzz"}}},
}

func TestArrayChunk(t *testing.T) {
for _, object := range testArrayChunk {
res := pgo.ArrayChunk(object.array, object.size)

s := reflect.ValueOf(object.result)
len := s.Len()
for i := 0; i < len; i++ {
array := s.Index(i).Interface()

ss := reflect.ValueOf(array)
arrLen := ss.Len()

result := reflect.ValueOf(res[i])
for j := 0; j < arrLen; j++ {
if result.Index(j).Interface() != ss.Index(j).Interface() {
t.Fatalf("Want: %v, got: %v", result.Index(j).Interface(), ss.Index(j).Interface())
}
}
}
}
}

0 comments on commit 7f191b2

Please sign in to comment.