Skip to content

Commit

Permalink
Merge pull request #13 from tideland/12-fix-late-discoverings
Browse files Browse the repository at this point in the history
Fixes in deleting and sorting
  • Loading branch information
themue authored Aug 20, 2022
2 parents 132f22a + dc1d9be commit d23b309
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 64 deletions.
14 changes: 0 additions & 14 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,6 @@ func DeleteAll[V comparable](dv V, ivs []V) []V {
return ovs
}

// DeleteAllWith removes all matching valus of a slice where pred returns true.
func DeleteAllWith[V any](pred func(V) bool, ivs []V) []V {
if ivs == nil {
return nil
}
ovs := []V{}
for _, v := range ivs {
if !pred(v) {
ovs = append(ovs, v)
}
}
return ovs
}

// DropWhile removes all values as long pred() returns true.
func DropWhile[V any](pred func(V) bool, ivs []V) []V {
if ivs == nil {
Expand Down
44 changes: 0 additions & 44 deletions slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,50 +215,6 @@ func TestDeleteAll(t *testing.T) {
}
}

// TestDeleteAllWith verifies the deleting of all matching values of a
// slice where pred returns true.
func TestDeleteAllWith(t *testing.T) {
assert := asserts.NewTesting(t, asserts.FailStop)

shallDelete := func(v int) bool { return v == 5 }
tests := []struct {
descr string
values []int
out []int
}{
{
descr: "Delete single value",
values: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
out: []int{1, 2, 3, 4, 6, 7, 8, 9},
}, {
descr: "Delete multiple values",
values: []int{1, 2, 5, 3, 4, 5, 5, 5, 5, 6, 7, 5, 8, 9},
out: []int{1, 2, 3, 4, 6, 7, 8, 9},
}, {
descr: "Delete uncontained values",
values: []int{1, 2, 3, 4, 6, 7, 8, 9},
out: []int{1, 2, 3, 4, 6, 7, 8, 9},
}, {
descr: "Delete all values",
values: []int{5, 5, 5, 5, 5},
out: []int{},
}, {
descr: "Delete in empty input",
values: []int{},
out: []int{},
}, {
descr: "Delete in nil input",
values: nil,
out: nil,
},
}

for _, test := range tests {
assert.Logf(test.descr)
assert.Equal(slices.DeleteAllWith(shallDelete, test.values), test.out)
}
}

// TestDropWhile verifies the dropping of the slice elements as long
// as a test returns true.
func TestDropWhile(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

package slices // import "tideland.dev/go/slices"

//--------------------
// IMPORTS
//--------------------

import (
"runtime"

Expand All @@ -24,13 +28,13 @@ func Sort[V constraints.Ordered](ivs []V) []V {
return vs[i] < vs[j]
}

return SortWith(ivs, less)
return SortWith(less, ivs)
}

// SortWith sorts a slice based on a less function comparing the two values
// at the indexes i and j and returning true if the value at i has to be sorted
// before the one at j.
func SortWith[V any](ivs []V, less func(vs []V, i, j int) bool) []V {
func SortWith[V any](less func(vs []V, i, j int) bool, ivs []V) []V {
ovs := Copy(ivs)

sort(ovs, less)
Expand All @@ -50,7 +54,7 @@ func IsSorted[V constraints.Ordered](vs []V) bool {

// IsSortedWith returns true if a slice is sorted in ascending order
// using less as comparison function.
func IsSortedWith[V any](vs []V, less func(a, b V) bool) bool {
func IsSortedWith[V any](less func(a, b V) bool, vs []V) bool {
for i := len(vs) - 1; i > 0; i-- {
if less(vs[i], vs[i-1]) {
return false
Expand Down
6 changes: 3 additions & 3 deletions sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestSortWith(t *testing.T) {

for _, test := range tests {
assert.Logf(test.descr)
assert.Equal(slices.SortWith(test.values, less), test.out)
assert.Equal(slices.SortWith(less, test.values), test.out)
}
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func TestIsSortedWith(t *testing.T) {

for _, test := range tests {
assert.Logf(test.descr)
assert.Equal(slices.IsSortedWith(test.values, less), test.out)
assert.Equal(slices.IsSortedWith(less, test.values), test.out)
}
}

Expand All @@ -220,7 +220,7 @@ func BenchmarkSortWith(b *testing.B) {
vs := gen.Words(10000)
less := func(vs []string, i, j int) bool { return len(vs[i]) < len(vs[j]) }

slices.SortWith(vs, less)
slices.SortWith(less, vs)
}

// FuzzSort runs a fuzz test on the standard sorting.
Expand Down

0 comments on commit d23b309

Please sign in to comment.