From 1df76cfaa459771a237bc1f5f87a93598355e5ad Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Fri, 9 Oct 2020 10:49:41 -0500 Subject: [PATCH] Rename rotate(at:) to rotate(toStartAt:) This was the intended name for the rotate method, just missed in the initial release. --- Guides/Rotate.md | 19 +++++----- Sources/Algorithms/Partition.swift | 2 +- Sources/Algorithms/Rotate.swift | 37 +++++++++++++++----- Tests/SwiftAlgorithmsTests/RotateTests.swift | 4 +-- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/Guides/Rotate.md b/Guides/Rotate.md index bf59918..c567f2c 100644 --- a/Guides/Rotate.md +++ b/Guides/Rotate.md @@ -7,7 +7,7 @@ A mutating method that rotates the elements of a collection to new positions. ```swift var numbers = [10, 20, 30, 40, 50, 60] -let p = numbers.rotate(at: 2) +let p = numbers.rotate(toStartAt: 2) // numbers == [30, 40, 50, 60, 10, 20] // p == 4 -- numbers[p] == 10 ``` @@ -18,9 +18,9 @@ variants that take a range: ```swift var numbers = [10, 20, 30, 40, 50, 60] -numbers.rotate(subrange: 0..<3, at: 1) +numbers.rotate(subrange: 0..<3, toStartAt: 1) // numbers = [20, 30, 10, 40, 50, 60] -numbers.rotate(subrange: 3..<6, at: 4) +numbers.rotate(subrange: 3..<6, toStartAt: 4) // numbers = [20, 30, 10, 50, 60, 40] ``` @@ -30,11 +30,11 @@ This adds the two `MutableCollection` methods shown above: ```swift extension MutableCollection { - mutating func rotate(at p: Index) -> Index + mutating func rotate(toStartAt p: Index) -> Index mutating func rotate( subrange: Range, - at p: Index + toStartAt p: Index ) -> Index } ``` @@ -49,13 +49,14 @@ the number of swaps required per element, so `rotate` would need to be a ### Naming The index parameter has been proposed as `shiftingToStart` in the past; this -proposal uses the simpler `at` label. `shiftingToStart` introduces the idea of -a "shift", which can sound like shifting just that single element to the +version uses the `toStartAt` label, instead. `shiftingToStart` introduces the +idea of a "shift", which can sound like shifting just that single element to the beginning of the collection. For the range-based overloads, the label could be omitted. That is, instead of using `subrange:`, the method could be called as -`numbers.rotate(0..<3, at: 2)`. +`numbers.rotate(0..<3, toStartAt: 2)`. The label is included here for +consistency with other range-based mutating methods. ### Comparison with other languages @@ -65,5 +66,5 @@ semantics to this one. **Ruby:** You can rotate the elements of an array by a number of positions, either forward or backward (by passing a negative number). For zero-indexed collections, forward rotation by e.g. 3 elements is equivalent to -`rotate(at: 3)`. +`rotate(toStartAt: 3)`. diff --git a/Sources/Algorithms/Partition.swift b/Sources/Algorithms/Partition.swift index 603a3e5..0454c5d 100644 --- a/Sources/Algorithms/Partition.swift +++ b/Sources/Algorithms/Partition.swift @@ -42,7 +42,7 @@ extension MutableCollection { count: n - h, subrange: i.., - at newStart: Index + toStartAt newStart: Index ) -> Index { var m = newStart, s = subrange.lowerBound let e = subrange.upperBound @@ -160,8 +160,8 @@ extension MutableCollection { } @discardableResult - public mutating func rotate(at newStart: Index) -> Index { - rotate(subrange: startIndex.. Index { + rotate(subrange: startIndex.., - at newStart: Index + toStartAt newStart: Index ) -> Index { reverse(subrange: subrange.lowerBound.. Index { - rotate(subrange: startIndex.. Index { + rotate(subrange: startIndex.., + at newStart: Index) -> Index + { + rotate(subrange: subrange, toStartAt: newStart) + } + + @available(*, deprecated, renamed: "rotate(toStartAt:)") + @discardableResult + public mutating func rotate(at newStart: Index) -> Index { + rotate(toStartAt: newStart) } } diff --git a/Tests/SwiftAlgorithmsTests/RotateTests.swift b/Tests/SwiftAlgorithmsTests/RotateTests.swift index 0cd1151..c37b5ea 100644 --- a/Tests/SwiftAlgorithmsTests/RotateTests.swift +++ b/Tests/SwiftAlgorithmsTests/RotateTests.swift @@ -18,9 +18,9 @@ final class RotateTests: XCTestCase { let a = Array(0..