Skip to content

slices: add Reverse #58565

@adonovan

Description

@adonovan

During the review of the proposal for the new slices package I mentioned that Reverse, a common slice operation, seemed to be missing, and was advised to file a separate proposal. So here it is. I propose we add this function:

package slices

// Reverse reverses the elements of the slice in place.
func Reverse[S ~[]E, E any](s S) {
       for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
               s[i], s[j] = s[j], s[i]
       }
}

You can see the full implementation in https://go.dev/cl/468855. ;-)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions