Skip to content

Commit

Permalink
slice: deprecate Reverse, shim to the equivalent stdlib slices.Reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
danderson committed Jul 26, 2024
1 parent d711d76 commit 8488bbe
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 40 deletions.
4 changes: 3 additions & 1 deletion mdiff/mdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
package mdiff

import (
"slices"

"github.com/creachadair/mds/slice"
)

Expand Down Expand Up @@ -189,7 +191,7 @@ func (d *Diff) findContext(c *Chunk, n int) (pre, post []string) {
}
pre = append(pre, d.Left[p]) // they are equal, so pick one
}
slice.Reverse(pre) // we walked backward from the start
slices.Reverse(pre) // we walked backward from the start

for i := 0; i < n; i++ {
p, q := lend+i, rend+i
Expand Down
7 changes: 5 additions & 2 deletions slice/edit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package slice

import "fmt"
import (
"fmt"
"slices"
)

// LCS computes a longest common subsequence of as and bs.
//
Expand Down Expand Up @@ -64,7 +67,7 @@ func LCSFunc[T any, Slice ~[]T](as, bs Slice, eq func(a, b T) bool) Slice {
for p := c[len(as)]; p.n > 0; p = p.prev {
out = append(out, as[p.i])
}
Reverse(out)
slices.Reverse(out)
return out
}

Expand Down
10 changes: 0 additions & 10 deletions slice/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ func ExampleDedup() {
// [1 3 2 4 5 1 3]
}

func ExampleReverse() {
vs := []string{"red", "yellow", "blue", "green"}
fmt.Println("before:", strings.Join(vs, " "))
slice.Reverse(vs)
fmt.Println("after:", strings.Join(vs, " "))
// Ouput:
// before: red yellow blue green
// after: green blue yellow red
}

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

Expand Down
7 changes: 3 additions & 4 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ func Dedup[T comparable](vs []T) []T {
}

// Reverse reverses the contents of vs in-place.
//
// Deprecated: Use the equivalent [slices.Reverse] instead.
func Reverse[T any, Slice ~[]T](vs Slice) {
for i, j := 0, len(vs)-1; i < j; i++ {
vs[i], vs[j] = vs[j], vs[i]
j--
}
slices.Reverse(vs)
}

// Zero sets all the elements of vs to their zero value.
Expand Down
23 changes: 0 additions & 23 deletions slice/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,6 @@ func TestPartition(t *testing.T) {
}
}

func TestReverse(t *testing.T) {
tests := []struct {
name string
input []int
want []int
}{
{"Nil", nil, nil},
{"Empty", []int{}, nil},
{"Single", []int{11}, []int{11}},
{"Multiple", []int{1, 2, 3, 4, 5}, []int{5, 4, 3, 2, 1}},
{"Palindrome", []int{1, 2, 3, 2, 1}, []int{1, 2, 3, 2, 1}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cp := append([]int(nil), tc.input...)
slice.Reverse(cp)
if diff := cmp.Diff(tc.want, cp); diff != "" {
t.Errorf("Reverse(%v) result (-want, +got)\n%s", tc.input, diff)
}
})
}
}

func TestZero(t *testing.T) {
zs := []int{1, 2, 3, 4, 5}
slice.Zero(zs[3:])
Expand Down

0 comments on commit 8488bbe

Please sign in to comment.