diff --git a/slice/example_test.go b/slice/example_test.go index 87268c1..0fd81ec 100644 --- a/slice/example_test.go +++ b/slice/example_test.go @@ -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} diff --git a/slice/slice.go b/slice/slice.go index 431a1f9..e3f39bb 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -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 diff --git a/slice/slice_test.go b/slice/slice_test.go index 71c9b23..05b321c 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -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 }