Skip to content

Add BatchListIntoGroupsOf #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions collections/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,29 @@ func RemoveElementFromList(list []string, element string) []string {
return out
}

// MakeCopyOfList will return a new list that is a copy of the given list.
func MakeCopyOfList(list []string) []string {
copyOfList := make([]string, len(list))
copy(copyOfList, list)
return copyOfList
}

// BatchListIntoGroupsOf will group the provided string slice into groups of size n, with the last of being truncated to
// the remaining count of strings. Returns nil if n is <= 0
func BatchListIntoGroupsOf(slice []string, batchSize int) [][]string {
if batchSize <= 0 {
return nil
}

// Taken from SliceTricks: https://github.com/golang/go/wiki/SliceTricks#batching-with-minimal-allocation
// Intuition: We repeatedly slice off batchSize elements from slice and append it to the output, until there
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I didn't realize they had a batch example right in there 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Saves me the trouble :)

// is not enough.
output := [][]string{}
for batchSize < len(slice) {
slice, output = slice[batchSize:], append(output, slice[0:batchSize:batchSize])
}
if len(slice) > 0 {
output = append(output, slice)
}
return output
}
81 changes: 78 additions & 3 deletions collections/lists_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package collections

import (
"testing"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMakeCopyOfListMakesACopy(t *testing.T) {
original := []string{"foo", "bar", "baz"}
copyOfList := MakeCopyOfList(original)
assert.Equal(t, original, copyOfList)
}

func TestListContainsElement(t *testing.T) {
t.Parallel()

testCases := []struct {
list []string
list []string
element string
expected bool
}{
Expand All @@ -31,7 +38,7 @@ func TestRemoveElementFromList(t *testing.T) {
t.Parallel()

testCases := []struct {
list []string
list []string
element string
expected []string
}{
Expand All @@ -50,3 +57,71 @@ func TestRemoveElementFromList(t *testing.T) {
}
}

func TestBatchListIntoGroupsOf(t *testing.T) {
t.Parallel()

testCases := []struct {
stringList []string
n int
result [][]string
}{
{
[]string{"macaroni", "gentoo", "magellanic", "adelie", "little", "king", "emperor"},
2,
[][]string{
[]string{"macaroni", "gentoo"},
[]string{"magellanic", "adelie"},
[]string{"little", "king"},
[]string{"emperor"},
},
},
{
[]string{"macaroni", "gentoo", "magellanic", "adelie", "king", "emperor"},
2,
[][]string{
[]string{"macaroni", "gentoo"},
[]string{"magellanic", "adelie"},
[]string{"king", "emperor"},
},
},
{
[]string{"macaroni", "gentoo", "magellanic"},
5,
[][]string{
[]string{"macaroni", "gentoo", "magellanic"},
},
},
{
[]string{"macaroni", "gentoo", "magellanic"},
5,
[][]string{
[]string{"macaroni", "gentoo", "magellanic"},
},
},
{
[]string{"macaroni", "gentoo", "magellanic"},
-1,
nil,
},
{
[]string{"macaroni", "gentoo", "magellanic"},
0,
nil,
},
{
[]string{},
7,
[][]string{},
},
}

for idx, testCase := range testCases {
t.Run(fmt.Sprintf("%s_%d", t.Name(), idx), func(t *testing.T) {
t.Parallel()
original := MakeCopyOfList(testCase.stringList)
assert.Equal(t, BatchListIntoGroupsOf(testCase.stringList, testCase.n), testCase.result)
// Make sure the function doesn't modify the original list
assert.Equal(t, testCase.stringList, original)
})
}
}