// ReversePartial reverses a portion of a slice in place from start to end (-1 for end of list) indices.
func ReversePartial[T any](arr []T, start, end int) error {
if end == -1 {
end = len(arr) - 1
}
// Validate indices
if start < 0 || end >= len(arr) || start >= end {
return fmt.Errorf("Invalid start or end indices")
}
for start < end {
arr[start], arr[end] = arr[end], arr[start] // Swap elements
start++
end--
}
return nil
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter