1
0

sliceutil.go 701 B

123456789101112131415161718192021222324
  1. // Copyright (C) 2023 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package sliceutil
  7. // RemoveAndZero removes the element at index i from slice s and returns the
  8. // resulting slice. The slice ordering is preserved; the last slice element
  9. // is zeroed before shrinking.
  10. func RemoveAndZero[E any, S ~[]E](s S, i int) S {
  11. copy(s[i:], s[i+1:])
  12. s[len(s)-1] = *new(E)
  13. return s[:len(s)-1]
  14. }
  15. func Map[E, R any, S ~[]E](s S, f func(E) R) []R {
  16. r := make([]R, len(s))
  17. for i, v := range s {
  18. r[i] = f(v)
  19. }
  20. return r
  21. }