Skip to content

Commit 1df76cf

Browse files
committed
Rename rotate(at:) to rotate(toStartAt:)
This was the intended name for the rotate method, just missed in the initial release.
1 parent 7188edf commit 1df76cf

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

Guides/Rotate.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A mutating method that rotates the elements of a collection to new positions.
77

88
```swift
99
var numbers = [10, 20, 30, 40, 50, 60]
10-
let p = numbers.rotate(at: 2)
10+
let p = numbers.rotate(toStartAt: 2)
1111
// numbers == [30, 40, 50, 60, 10, 20]
1212
// p == 4 -- numbers[p] == 10
1313
```
@@ -18,9 +18,9 @@ variants that take a range:
1818

1919
```swift
2020
var numbers = [10, 20, 30, 40, 50, 60]
21-
numbers.rotate(subrange: 0..<3, at: 1)
21+
numbers.rotate(subrange: 0..<3, toStartAt: 1)
2222
// numbers = [20, 30, 10, 40, 50, 60]
23-
numbers.rotate(subrange: 3..<6, at: 4)
23+
numbers.rotate(subrange: 3..<6, toStartAt: 4)
2424
// numbers = [20, 30, 10, 50, 60, 40]
2525
```
2626

@@ -30,11 +30,11 @@ This adds the two `MutableCollection` methods shown above:
3030

3131
```swift
3232
extension MutableCollection {
33-
mutating func rotate(at p: Index) -> Index
33+
mutating func rotate(toStartAt p: Index) -> Index
3434

3535
mutating func rotate(
3636
subrange: Range<Index>,
37-
at p: Index
37+
toStartAt p: Index
3838
) -> Index
3939
}
4040
```
@@ -49,13 +49,14 @@ the number of swaps required per element, so `rotate` would need to be a
4949
### Naming
5050

5151
The index parameter has been proposed as `shiftingToStart` in the past; this
52-
proposal uses the simpler `at` label. `shiftingToStart` introduces the idea of
53-
a "shift", which can sound like shifting just that single element to the
52+
version uses the `toStartAt` label, instead. `shiftingToStart` introduces the
53+
idea of a "shift", which can sound like shifting just that single element to the
5454
beginning of the collection.
5555

5656
For the range-based overloads, the label could be omitted. That is, instead of
5757
using `subrange:`, the method could be called as
58-
`numbers.rotate(0..<3, at: 2)`.
58+
`numbers.rotate(0..<3, toStartAt: 2)`. The label is included here for
59+
consistency with other range-based mutating methods.
5960

6061
### Comparison with other languages
6162

@@ -65,5 +66,5 @@ semantics to this one.
6566
**Ruby:** You can rotate the elements of an array by a number of positions,
6667
either forward or backward (by passing a negative number). For zero-indexed
6768
collections, forward rotation by e.g. 3 elements is equivalent to
68-
`rotate(at: 3)`.
69+
`rotate(toStartAt: 3)`.
6970

Sources/Algorithms/Partition.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension MutableCollection {
4242
count: n - h,
4343
subrange: i..<subrange.upperBound,
4444
by: belongsInSecondPartition)
45-
return rotate(subrange: j..<k, at: i)
45+
return rotate(subrange: j..<k, toStartAt: i)
4646
}
4747

4848
/// Moves all elements satisfying the given predicate into a suffix of the

Sources/Algorithms/Rotate.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extension MutableCollection where Self: BidirectionalCollection {
5757
}
5858

5959
//===----------------------------------------------------------------------===//
60-
// rotate(at:) / rotate(subrange:at:)
60+
// rotate(toStartAt:) / rotate(subrange:toStartAt:)
6161
//===----------------------------------------------------------------------===//
6262

6363
extension MutableCollection {
@@ -101,7 +101,7 @@ extension MutableCollection {
101101
@discardableResult
102102
public mutating func rotate(
103103
subrange: Range<Index>,
104-
at newStart: Index
104+
toStartAt newStart: Index
105105
) -> Index {
106106
var m = newStart, s = subrange.lowerBound
107107
let e = subrange.upperBound
@@ -160,16 +160,16 @@ extension MutableCollection {
160160
}
161161

162162
@discardableResult
163-
public mutating func rotate(at newStart: Index) -> Index {
164-
rotate(subrange: startIndex..<endIndex, at: newStart)
163+
public mutating func rotate(toStartAt newStart: Index) -> Index {
164+
rotate(subrange: startIndex..<endIndex, toStartAt: newStart)
165165
}
166166
}
167167

168168
extension MutableCollection where Self: BidirectionalCollection {
169169
@discardableResult
170170
public mutating func rotate(
171171
subrange: Range<Index>,
172-
at newStart: Index
172+
toStartAt newStart: Index
173173
) -> Index {
174174
reverse(subrange: subrange.lowerBound..<newStart)
175175
reverse(subrange: newStart..<subrange.upperBound)
@@ -179,15 +179,15 @@ extension MutableCollection where Self: BidirectionalCollection {
179179
}
180180

181181
/// Rotates the elements of this collection so that the element
182-
/// at the specified index moves to the front of the collection.
182+
/// at the specified index becomes the start of the collection.
183183
///
184184
/// Rotating a collection is equivalent to breaking the collection into two
185185
/// sections at the index `newStart`, and then swapping those two sections.
186186
/// In this example, the `numbers` array is rotated so that the element at
187187
/// index `3` (`40`) is first:
188188
///
189189
/// var numbers = [10, 20, 30, 40, 50, 60, 70, 80]
190-
/// let oldStart = numbers.rotate(at: 3)
190+
/// let oldStart = numbers.rotate(toStartAt: 3)
191191
/// // numbers == [40, 50, 60, 70, 80, 10, 20, 30]
192192
/// // numbers[oldStart] == 10
193193
///
@@ -197,7 +197,26 @@ extension MutableCollection where Self: BidirectionalCollection {
197197
///
198198
/// - Complexity: O(*n*)
199199
@discardableResult
200+
public mutating func rotate(toStartAt newStart: Index) -> Index {
201+
rotate(subrange: startIndex..<endIndex, toStartAt: newStart)
202+
}
203+
}
204+
205+
// Deprecations
206+
207+
extension MutableCollection {
208+
@available(*, deprecated, renamed: "rotate(subrange:toStartAt:)")
209+
@discardableResult
210+
public mutating func rotate(
211+
subrange: Range<Index>,
212+
at newStart: Index) -> Index
213+
{
214+
rotate(subrange: subrange, toStartAt: newStart)
215+
}
216+
217+
@available(*, deprecated, renamed: "rotate(toStartAt:)")
218+
@discardableResult
200219
public mutating func rotate(at newStart: Index) -> Index {
201-
rotate(subrange: startIndex..<endIndex, at: newStart)
220+
rotate(toStartAt: newStart)
202221
}
203222
}

Tests/SwiftAlgorithmsTests/RotateTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ final class RotateTests: XCTestCase {
1818
let a = Array(0..<length)
1919
var b = a
2020
for j in 0..<length {
21-
let i = b.rotate(at: j)
21+
let i = b.rotate(toStartAt: j)
2222
XCTAssertEqualSequences(a[j...] + a[..<j], b)
23-
b.rotate(at: i)
23+
b.rotate(toStartAt: i)
2424
XCTAssertEqual(a, b)
2525
}
2626
}

0 commit comments

Comments
 (0)