rotate(subrange:toStartAt:)
Rotates the elements within the given subrange so that the element at the specified index becomes the start of the subrange.
@discardableResult mutating func rotate(subrange: Range<Index>, toStartAt newStart: Index) -> Index
Parameters
Returns
The new index of the element that was at the start of subrange
pre-rotation.
Rotating a collection is equivalent to breaking the collection into two sections at the index newStart
, and then swapping those two sections. In this example, the numbers
array is rotated so that the element at index 3
(40
) is first:
var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
let oldStart = numbers.rotate(subrange: 0..<4, toStartAt: 2)
// numbers == [30, 40, 10, 20, 50, 60, 70, 80]
// numbers[oldStart] == 10