]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/slices/slices.go
slices: add in-place Reverse function
[gostls13.git] / src / slices / slices.go
index 837863bacc9abde4483010bec52c58e4e1c9951d..7de00b342f0f6f49152270708c4fb9b05562cf5e 100644 (file)
@@ -445,3 +445,10 @@ func startIdx[S ~[]E, E any](haystack, needle S) int {
        // TODO: what if the overlap is by a non-integral number of Es?
        panic("needle not found")
 }
+
+// Reverse reverses the elements of the slice in place.
+func Reverse[E any](s []E) {
+       for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
+               s[i], s[j] = s[j], s[i]
+       }
+}