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

42 lines
607 B

2 years ago
  1. package gentools
  2. func IndexOf[T comparable](arr []T, value T) int {
  3. for i, v := range arr {
  4. if v == value {
  5. return i
  6. }
  7. }
  8. return -1
  9. }
  10. func AddUniques[T comparable](arr *[]T, values ...T) {
  11. Outer:
  12. for _, v := range values {
  13. for _, v2 := range *arr {
  14. if v2 == v {
  15. continue Outer
  16. }
  17. }
  18. *arr = append(*arr, v)
  19. }
  20. }
  21. func Map[T any, U any](arr []T, cb func(T) U) []U {
  22. arr2 := make([]U, len(arr))
  23. for i, v := range arr {
  24. arr2[i] = cb(v)
  25. }
  26. return arr2
  27. }
  28. func Flatten[T any](arr [][]T) []T {
  29. arr2 := make([]T, 0, 128)
  30. for _, sub := range arr {
  31. arr2 = sub
  32. }
  33. return arr2
  34. }