github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/lists/remove.go (about)

     1  package lists
     2  
     3  import "fmt"
     4  
     5  func RemoveOrdered[T any](slice []T, i int) ([]T, error) {
     6  	switch {
     7  	case i >= len(slice):
     8  		fallthrough
     9  	case i < 0:
    10  		return nil, fmt.Errorf("index out of bounds: len=%d, index=%d", len(slice), i)
    11  	default:
    12  		return append(slice[:i], slice[i+1:]...), nil
    13  	}
    14  }
    15  
    16  func RemoveUnordered[T any](slice []T, i int) ([]T, error) {
    17  	switch {
    18  	case i >= len(slice):
    19  		fallthrough
    20  	case i < 0:
    21  		return nil, fmt.Errorf("index out of bounds: len=%d, index=%d", len(slice), i)
    22  	default:
    23  		slice[i] = slice[len(slice)-1]
    24  		return slice[:len(slice)-1], nil
    25  	}
    26  }