test-errcode
withchao 2 years ago
parent 778e3e5cd2
commit e8a24ab59e

@ -1,4 +1,4 @@
package utilsv2 package utils
import ( import (
"sort" "sort"
@ -26,8 +26,8 @@ func Distinct[T comparable](ts []T) []T {
}) })
} }
// DeleteAt delete slice element, support negative number to delete the penultimate // Delete delete slice element, support negative number to delete the penultimate
func DeleteAt[E any](es []E, index ...int) []E { func Delete[E any](es []E, index ...int) []E {
switch len(index) { switch len(index) {
case 0: case 0:
return es return es
@ -58,6 +58,13 @@ func DeleteAt[E any](es []E, index ...int) []E {
} }
} }
// DeleteAt delete slice element, support negative number to delete the penultimate
func DeleteAt[E any](es *[]E, index ...int) []E {
v := Delete(*es, index...)
*es = v
return v
}
// IndexAny get the index of the element // IndexAny get the index of the element
func IndexAny[E any, K comparable](es []E, e E, fn func(e E) K) int { func IndexAny[E any, K comparable](es []E, e E, fn func(e E) K) int {
k := fn(e) k := fn(e)

@ -1,4 +1,4 @@
package utilsv2 package utils
import ( import (
"fmt" "fmt"
@ -12,9 +12,9 @@ func TestDistinct(t *testing.T) {
func TestDeleteAt(t *testing.T) { func TestDeleteAt(t *testing.T) {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(DeleteAt(arr, 0, 1, -1, -2)) fmt.Println(Delete(arr, 0, 1, -1, -2))
fmt.Println(DeleteAt(arr)) fmt.Println(Delete(arr))
fmt.Println(DeleteAt(arr, 1)) fmt.Println(Delete(arr, 1))
} }
func TestSliceToMap(t *testing.T) { func TestSliceToMap(t *testing.T) {
@ -72,8 +72,8 @@ func TestCompleteAny(t *testing.T) {
}) })
} }
list = DeleteAt(list, -1) DeleteAt(&list, -1)
ids = DeleteAt(ids, -1) DeleteAt(&ids, -1)
ok := CompleteAny(ids, list, func(t Item) int { ok := CompleteAny(ids, list, func(t Item) int {
return t.ID return t.ID
Loading…
Cancel
Save