Skip to content
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

Feature/partition in 2 #499

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 20 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,26 @@ func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee fun
// return Values[K, []T](groups)
}

// PartitionIn2By returns two array of elements. The first returned array always contains the elements of
// the collection for which the iteratee function returned true, and the second the rest. This function is
// similar to PartitionBy, but guarantees that the output is going to contain two groups, though one may be
// with no elements. This is useful because removes the need to check if more than one group was returned, and
// also guarantees the order of the returned groups.
func PartitionIn2By[T any, Slice ~[]T](collection Slice, iteratee func(item T) bool) (Slice, Slice) {
trueSlice := Slice{}
falseSlice := Slice{}

for _, item := range collection {
if iteratee(item) {
trueSlice = append(trueSlice, item)
} else {
falseSlice = append(falseSlice, item)
}
}

return trueSlice, falseSlice
}

// Flatten returns an array a single level deep.
// Play: https://go.dev/play/p/rbp9ORaMpjw
func Flatten[T any, Slice ~[]T](collection []Slice) Slice {
Expand Down
12 changes: 12 additions & 0 deletions slice_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ func ExamplePartitionBy() {
// [1 3]
}

func ExamplePartitionIn2By() {
list := []int{-2, -1, 0, 1, 2}

negative, positive := PartitionIn2By(list, func(x int) bool { return x < 0 })

fmt.Printf("%v\n", negative)
fmt.Printf("%v\n", positive)
// Output:
// [-2 -1]
// [0 1 2]
}

func ExampleFlatten() {
list := [][]int{{0, 1, 2}, {3, 4, 5}}

Expand Down
20 changes: 20 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ func TestPartitionBy(t *testing.T) {
})
is.IsType(nonempty[0], allStrings, "type preserved")
}
func TestPartitionIn2By(t *testing.T) {
t.Parallel()
is := assert.New(t)

isNegative := func(x int) bool { return x < 0 }

negative1, positive1 := PartitionIn2By([]int{-2, -1, 0, 1, 2}, isNegative)
negative2, positive2 := PartitionIn2By([]int{}, isNegative)

is.Equal(negative1, []int{-2, -1})
is.Equal(positive1, []int{0, 1, 2})

is.Equal(negative2, []int{})
is.Equal(positive2, []int{})

type myStrings []string
allStrings := myStrings{"", "foo", "bar"}
nonempty, _ := PartitionIn2By(allStrings, func(item string) bool { return len(item) != 0 })
is.IsType(nonempty, allStrings, "type preserved")
}

func TestFlatten(t *testing.T) {
t.Parallel()
Expand Down
Loading