Skip to content

Commit

Permalink
add example and test code
Browse files Browse the repository at this point in the history
  • Loading branch information
dsolerh committed Jul 18, 2024
1 parent 9306682 commit 664ed38
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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

0 comments on commit 664ed38

Please sign in to comment.