Skip to content

Commit

Permalink
slicehelpers: Add Partition
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Sep 8, 2024
1 parent 23758fd commit 5ab44b6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
19 changes: 19 additions & 0 deletions slicehelpers/slicehelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ func Chunk[T any](s []T, n int) [][]T {
}
return partitions
}

// Parition partitions s into slices of size size.

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

comment on exported function Partition should be of the form "Partition ..."

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

comment on exported function Partition should be of the form "Partition ..."

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

comment on exported function Partition should be of the form "Partition ..."

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

comment on exported function Partition should be of the form "Partition ..."

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

comment on exported function Partition should be of the form "Partition ..."

Check failure on line 28 in slicehelpers/slicehelpers.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, windows-latest)

comment on exported function Partition should be of the form "Partition ..."
func Partition[T any](s []T, size int) [][]T {
if len(s) == 0 {
return nil
}
if size <= 0 {
return nil
}
var partitions [][]T
for i := 0; i < len(s); i += size {
end := i + size
if end > len(s) {
end = len(s)
}
partitions = append(partitions, s[i:end])
}
return partitions
}
69 changes: 69 additions & 0 deletions slicehelpers/slicehelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,72 @@ func TestChunk(t *testing.T) {
},
)
}

func TestPartition(t *testing.T) {
c := qt.New(t)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 2),
qt.DeepEquals,
[][]int{
{1, 2},
{3, 4},
{5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 3),
qt.DeepEquals,
[][]int{
{1, 2, 3},
{4, 5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 1),
qt.DeepEquals,
[][]int{
{1},
{2},
{3},
{4},
{5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 5),
qt.DeepEquals,
[][]int{
{1, 2, 3, 4, 5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 6),
qt.DeepEquals,
[][]int{
{1, 2, 3, 4, 5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 7),
qt.DeepEquals,
[][]int{
{1, 2, 3, 4, 5},
},
)

c.Assert(
Partition([]int{1, 2, 3, 4, 5}, 0),
qt.IsNil,
)

c.Assert(
Partition([]int{}, 2),
qt.IsNil,
)
}

0 comments on commit 5ab44b6

Please sign in to comment.