| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package moreslices |
| 6 | |
| 7 | // Remove removes all values equal to elem from slice. |
| 8 | // |
| 9 | // The closest equivalent in the standard slices package is: |
| 10 | // |
| 11 | // DeleteFunc(func(x T) bool { return x == elem }) |
| 12 | func Remove[T comparable](slice []T, elem T) []T { |
| 13 | out := slice[:0] |
| 14 | for _, v := range slice { |
| 15 | if v != elem { |
| 16 | out = append(out, v) |
| 17 | } |
| 18 | } |
| 19 | return out |
| 20 | } |
| 21 | |
| 22 | // ConvertStrings converts a slice of type A (with underlying type string) |
| 23 | // to a slice of type B (with underlying type string). |
| 24 | func ConvertStrings[B, A ~string](input []A) []B { |
| 25 | result := make([]B, len(input)) |
| 26 | for i, v := range input { |
| 27 | result[i] = B(string(v)) |
| 28 | } |
| 29 | return result |
| 30 | } |