Skip to content

Commit

Permalink
slice: remove the Split function
Browse files Browse the repository at this point in the history
  • Loading branch information
creachadair committed Nov 11, 2024
1 parent 244c819 commit ae65dbf
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 62 deletions.
14 changes: 0 additions & 14 deletions slice/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ func ExamplePartition() {
// [3 1 9 5 7]
}

func ExampleSplit() {
vs := []int{1, 2, 3, 4, 5, 6, 7, 8}

fmt.Println("input:", vs)
first3, tail := slice.Split(vs, 3)
head, last3 := slice.Split(vs, -3)
fmt.Println("first3:", first3, "tail:", tail)
fmt.Println("last3:", last3, "head:", head)
// Output:
// input: [1 2 3 4 5 6 7 8]
// first3: [1 2 3] tail: [4 5 6 7 8]
// last3: [6 7 8] head: [1 2 3 4 5]
}

func ExampleMatchingKeys() {
vs := map[string]int{"red": 3, "yellow": 6, "blue": 4, "green": 5}

Expand Down
12 changes: 0 additions & 12 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,6 @@ func MapKeys[T comparable, U any](m map[T]U) []T {
return keys
}

// Split returns two subslices of ss, the first containing the elements prior
// to index i, the second containing the elements from index i to the end.
// If i < 0, offsets are counted backward from the end. If i is out of range,
// Split will panic.
func Split[T any, Slice ~[]T](ss Slice, i int) (lhs, rhs Slice) {
b, ok := sliceCheck(i, len(ss))
if !ok {
panic("index out of range")
}
return ss[:b], ss[b:]
}

func sliceCheck(i, n int) (int, bool) {
if i < 0 {
i += n
Expand Down
36 changes: 0 additions & 36 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,42 +103,6 @@ func TestMapKeys(t *testing.T) {
}
}

func TestSplit(t *testing.T) {
tests := []struct {
input string
i int
lhs, rhs []string
}{
{"", 0, nil, nil},
{"a", 0, nil, []string{"a"}},
{"a", 1, []string{"a"}, nil},
{"a b c", 0, nil, []string{"a", "b", "c"}},
{"a b c", 1, []string{"a"}, []string{"b", "c"}},
{"a b c", 2, []string{"a", "b"}, []string{"c"}},
{"a b c", 3, []string{"a", "b", "c"}, nil},
{"a b c d", -1, []string{"a", "b", "c"}, []string{"d"}},
{"a b c d", -2, []string{"a", "b"}, []string{"c", "d"}},
{"a b c d", -3, []string{"a"}, []string{"b", "c", "d"}},
{"a b c d", -4, nil, []string{"a", "b", "c", "d"}},
}
for _, tc := range tests {
input := strings.Fields(tc.input)
lhs, rhs := slice.Split(input, tc.i)
if diff := cmp.Diff(tc.lhs, lhs, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("Split %q %d lhs (-want, +got):\n%s", input, tc.i, diff)
}
if diff := cmp.Diff(tc.rhs, rhs, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("Split %q %d rhs (-want, +got):\n%s", input, tc.i, diff)
}
}
t.Run("Range", func(t *testing.T) {
mtest.MustPanic(t, func() { slice.Split([]string(nil), 1) })
mtest.MustPanic(t, func() { slice.Split([]string(nil), -1) })
mtest.MustPanic(t, func() { slice.Split([]int{1, 2, 3}, 4) })
mtest.MustPanic(t, func() { slice.Split([]int{1, 2, 3}, -4) })
})
}

func TestMatchingKeys(t *testing.T) {
even := func(z int) bool { return z%2 == 0 }
big := func(z int) bool { return z > 10 }
Expand Down

0 comments on commit ae65dbf

Please sign in to comment.