diff --git a/slices.go b/slices.go index d49b432..7e8ad7e 100644 --- a/slices.go +++ b/slices.go @@ -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 { diff --git a/slices_test.go b/slices_test.go index 66744d3..110566f 100644 --- a/slices_test.go +++ b/slices_test.go @@ -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) { diff --git a/sort.go b/sort.go index e2cc067..33d97c8 100644 --- a/sort.go +++ b/sort.go @@ -7,6 +7,10 @@ package slices // import "tideland.dev/go/slices" +//-------------------- +// IMPORTS +//-------------------- + import ( "runtime" @@ -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) @@ -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 diff --git a/sort_test.go b/sort_test.go index 8bfc7d0..1dc4149 100644 --- a/sort_test.go +++ b/sort_test.go @@ -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) } } @@ -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) } } @@ -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.