You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lo.go 1.1 kB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package lo2
  2. import "github.com/samber/lo"
  3. func Remove[T comparable](arr []T, item T) []T {
  4. index := lo.IndexOf(arr, item)
  5. if index == -1 {
  6. return arr
  7. }
  8. return RemoveAt(arr, index)
  9. }
  10. func RemoveAll[T comparable](arr []T, item T) []T {
  11. return lo.Filter(arr, func(i T, idx int) bool {
  12. return i != item
  13. })
  14. }
  15. func RemoveAt[T any](arr []T, index int) []T {
  16. if index >= len(arr) {
  17. return arr
  18. }
  19. return append(arr[:index], arr[index+1:]...)
  20. }
  21. func RemoveAllDefault[T comparable](arr []T) []T {
  22. var def T
  23. return lo.Filter(arr, func(i T, idx int) bool {
  24. return i != def
  25. })
  26. }
  27. func Clear[T comparable](arr []T, item T) {
  28. var def T
  29. for i := 0; i < len(arr); i++ {
  30. if arr[i] == item {
  31. arr[i] = def
  32. }
  33. }
  34. }
  35. func ArrayClone[T any](arr []T) []T {
  36. return append([]T{}, arr...)
  37. }
  38. func Insert[T any](arr []T, index int, item T) []T {
  39. arr = append(arr, item)
  40. copy(arr[index+1:], arr[index:])
  41. arr[index] = item
  42. return arr
  43. }
  44. func Deref[T any](arr []*T) []T {
  45. result := make([]T, len(arr))
  46. for i := 0; i < len(arr); i++ {
  47. result[i] = *arr[i]
  48. }
  49. return result
  50. }