Skip to content

Commit d72b5ab

Browse files
gribozavrMax Moiseev
authored and
Max Moiseev
committed
func SequenceType.enumerate() => var SequenceType.enumerated
1 parent 09a6913 commit d72b5ab

10 files changed

+40
-44
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

+4-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public struct SourceLocStack {
105105
func _printStackTrace(stackTrace: SourceLocStack?) {
106106
guard let s = stackTrace where !s.locs.isEmpty else { return }
107107
print("stacktrace:")
108-
for (i, loc) in s.locs.reverse().enumerate() {
108+
for (i, loc) in s.locs.reverse().enumerated {
109109
let comment = (loc.comment != nil) ? " ; \(loc.comment!)" : ""
110110
print(" #\(i): \(loc.file):\(loc.line)\(comment)")
111111
}
@@ -2222,8 +2222,8 @@ public func checkRangeReplaceable<
22222222
// comparison later.
22232223
let source = Array<A.Iterator.Element>(makeCollection())
22242224

2225-
for (ix, i) in source.indices.enumerate() {
2226-
for (jx_, j) in (i..<source.endIndex).enumerate() {
2225+
for (ix, i) in source.indices.enumerated {
2226+
for (jx_, j) in (i..<source.endIndex).enumerated {
22272227
let jx = jx_ + ix
22282228

22292229
let oldCount = jx - ix
@@ -2250,7 +2250,7 @@ public func checkRangeReplaceable<
22502250
&a, "\(actualCount) != expected count \(expectedCount)")
22512251
}
22522252

2253-
for (kx, k) in a.indices.enumerate() {
2253+
for (kx, k) in a.indices.enumerated {
22542254
let expectedValue = kx < ix ? nth(source, kx)
22552255
: kx < jx + growth ? nth(newValues, kx - ix)
22562256
: nth(source, kx - growth)

stdlib/public/core/Algorithm.swift

+15-18
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,31 @@ public func max<T : Comparable>(x: T, _ y: T, _ z: T, _ rest: T...) -> T {
6666
return r
6767
}
6868

69-
/// The `IteratorProtocol` for `EnumeratedSequence`. `EnumeratedIterator`
70-
/// wraps a `Base` `IteratorProtocol` and yields successive `Int` values,
69+
/// The iterator for `EnumeratedSequence`. `EnumeratedIterator`
70+
/// wraps a `Base` iterator and yields successive `Int` values,
7171
/// starting at zero, along with the elements of the underlying
7272
/// `Base`:
7373
///
74-
/// var iterator = ["foo", "bar"].enumerate()
74+
/// var iterator = ["foo", "bar"].enumerated.iterator()
7575
/// iterator.next() // (0, "foo")
7676
/// iterator.next() // (1, "bar")
7777
/// iterator.next() // nil
78-
///
79-
/// - Note: Idiomatic usage is to call `enumerate` instead of
80-
/// constructing an `EnumeratedIterator` directly.
8178
public struct EnumeratedIterator<
8279
Base : IteratorProtocol
8380
> : IteratorProtocol, SequenceType {
84-
/// The type of element returned by `next()`.
85-
public typealias Element = (offset: Int, element: Base.Element)
86-
var _base: Base
87-
var _count: Int
81+
82+
internal var _base: Base
83+
internal var _count: Int
8884

8985
/// Construct from a `Base` iterator.
9086
internal init(_base: Base) {
9187
self._base = _base
9288
self._count = 0
9389
}
9490

91+
/// The type of element returned by `next()`.
92+
public typealias Element = (offset: Int, element: Base.Element)
93+
9594
/// Advance to the next element and return it, or `nil` if no next
9695
/// element exists.
9796
///
@@ -102,16 +101,14 @@ public struct EnumeratedIterator<
102101
}
103102
}
104103

105-
/// The `SequenceType` returned by `enumerate()`. `EnumeratedSequence`
106-
/// is a sequence of pairs (*n*, *x*), where *n*s are consecutive
107-
/// `Int`s starting at zero, and *x*s are the elements of a `Base`
108-
/// `SequenceType`:
104+
/// The type of the `enumerated` property.
109105
///
110-
/// var s = EnumeratedSequence(["foo", "bar"])
111-
/// Array(s) // [(0, "foo"), (1, "bar")]
106+
/// `EnumeratedSequence` is a sequence of pairs (*n*, *x*), where *n*s
107+
/// are consecutive `Int`s starting at zero, and *x*s are the elements
108+
/// of a `Base` `SequenceType`:
112109
///
113-
/// - Note: Idiomatic usage is to call `enumerate` instead of
114-
/// constructing an `EnumeratedSequence` directly.
110+
/// var s = ["foo", "bar"].enumerated
111+
/// Array(s) // [(0, "foo"), (1, "bar")]
115112
public struct EnumeratedSequence<Base : SequenceType> : SequenceType {
116113
internal var _base: Base
117114

stdlib/public/core/SequenceAlgorithms.swift.gyb

+4-5
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,23 @@ GElement = "Iterator.Element"
1919
}%
2020

2121
//===----------------------------------------------------------------------===//
22-
// enumerate()
22+
// enumerated
2323
//===----------------------------------------------------------------------===//
2424

2525
extension SequenceType {
2626
/// Return a lazy `SequenceType` containing pairs (*n*, *x*), where
2727
/// *n*s are consecutive `Int`s starting at zero, and *x*s are
28-
/// the elements of `base`:
28+
/// the elements of `self`:
2929
///
30-
/// > for (n, c) in "Swift".characters.enumerate() {
30+
/// > for (n, c) in "Swift".characters.enumerated {
3131
/// print("\(n): '\(c)'")
3232
/// }
3333
/// 0: 'S'
3434
/// 1: 'w'
3535
/// 2: 'i'
3636
/// 3: 'f'
3737
/// 4: 't'
38-
@warn_unused_result
39-
public func enumerate() -> EnumeratedSequence<Self> {
38+
public var enumerated: EnumeratedSequence<Self> {
4039
return EnumeratedSequence(_base: self)
4140
}
4241
}

test/IDE/complete_from_stdlib.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func testArchetypeReplacement2<BAR : Equatable>(a: [BAR]) {
142142
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/CurrNominal: popLast()[#Equatable?#]{{; name=.+}}
143143
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: dropFirst()[#ArraySlice<Equatable>#]{{; name=.+}}
144144
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: dropLast()[#ArraySlice<Equatable>#]{{; name=.+}}
145-
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: enumerate()[#EnumeratedSequence<[Equatable]>#]{{; name=.+}}
145+
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceVar]/Super: enumerated[#EnumeratedSequence<[Equatable]>#]{{; name=.+}}
146146
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: minElement({#(isOrderedBefore): (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
147147
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: maxElement({#(isOrderedBefore): (Equatable, Equatable) throws -> Bool##(Equatable, Equatable) throws -> Bool#})[' rethrows'][#Equatable?#]{{; name=.+}}
148148
// PRIVATE_NOMINAL_MEMBERS_6-DAG: Decl[InstanceMethod]/Super: reduce({#(initial): T#}, {#combine: (T, Equatable) throws -> T##(T, Equatable) throws -> T#})[' rethrows'][#T#]{{; name=.+}}

test/Interpreter/SDK/objc_implicit_unwrapped_bridge.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func testConvertArrayOfImplicitUnwrappedClass() {
3333

3434
// CHECK: Element 0 has value 1
3535
// CHECK: Element 1 has value 2
36-
for (index, obj) in classNSArr1.enumerate() {
36+
for (index, obj) in classNSArr1.enumerated {
3737
if let x = obj as? X {
3838
print("Element \(index) has value \(x.value)")
3939
} else {
@@ -60,7 +60,7 @@ func testConvertArrayOfImplicitUnwrappedValue() {
6060

6161
// CHECK: Element 0 has value Hello
6262
// CHECK: Element 1 has value World
63-
for (index, obj) in stringNSArr1.enumerate() {
63+
for (index, obj) in stringNSArr1.enumerated {
6464
if let str = obj as? String {
6565
print("Element \(index) has value \(str)")
6666
} else {
@@ -96,7 +96,7 @@ func testConvertArrayOfImplicitUnwrappedArray() {
9696
// CHECK: Welcome,
9797
// CHECK: Swift
9898
// CHECK: )
99-
for (index, obj) in nsarr.enumerate() {
99+
for (index, obj) in nsarr.enumerated {
100100
if let innerNSArr = obj as? NSArray {
101101
print("Element \(index) has value \(innerNSArr.description)")
102102
} else {
@@ -122,7 +122,7 @@ func testConvertToArrayOfImplicitUnwrappedClass() {
122122
// CHECK: Element 0 has value X(1)
123123
// CHECK: Element 1 has value X(2)
124124
print("Class array count = \(arr.count)")
125-
for (index, opt) in arr.enumerate() {
125+
for (index, opt) in arr.enumerated {
126126
if let x = opt {
127127
print("Element \(index) has value X(\(x.value))")
128128
} else {
@@ -146,7 +146,7 @@ func testConvertToArrayOfImplicitUnwrappedString() {
146146
// CHECK: Element 0 has value Hello
147147
// CHECK: Element 1 has value World
148148
print("String array count = \(arr.count)")
149-
for (index, opt) in arr.enumerate() {
149+
for (index, opt) in arr.enumerated {
150150
if let str = opt {
151151
print("Element \(index) has value \(str)")
152152
} else {

test/Interpreter/protocol_extensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ print(["a", "b", "c", "d", "e"].myIndexOf("d")!)
5858

5959
extension SequenceType {
6060
final public func myEnumerate() -> EnumeratedSequence<Self> {
61-
return self.enumerate()
61+
return self.enumerated
6262
}
6363
}
6464

validation-test/stdlib/ObjectiveC.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ ObjectiveCTests.test("NSObject/Hashable") {
3434
NSObjectWithCustomHashable(value: 20, hashValue: 100),
3535
NSObjectWithCustomHashable(value: 30, hashValue: 300),
3636
]
37-
for (i, object1) in objects.enumerate() {
38-
for (j, object2) in objects.enumerate() {
37+
for (i, object1) in objects.enumerated {
38+
for (j, object2) in objects.enumerated {
3939
checkHashable(
4040
object1._value == object2._value,
4141
object1,

validation-test/stdlib/SequenceType.swift.gyb

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var SequenceTypeTests = TestSuite("SequenceType")
6262
SequenceTypeTests.test("${algorithmKind}Element/WhereElementIsComparable") {
6363
for test in minMaxElementTests {
6464
let s = MinimalSequence<MinimalComparableValue>(
65-
elements: test.sequence.enumerate().map {
65+
elements: test.sequence.enumerated.map {
6666
MinimalComparableValue($1, identity: $0)
6767
})
6868
var maybeResult = s.${algorithmKind}Element()
@@ -89,7 +89,7 @@ SequenceTypeTests.test("${algorithmKind}Element/WhereElementIsComparable") {
8989
SequenceTypeTests.test("${algorithmKind}Element/Predicate") {
9090
for test in minMaxElementTests {
9191
let s = MinimalSequence<OpaqueValue<Int>>(
92-
elements: test.sequence.enumerate().map {
92+
elements: test.sequence.enumerated.map {
9393
OpaqueValue($1, identity: $0)
9494
})
9595
var timesClosureWasCalled = 0
@@ -234,7 +234,7 @@ SequenceTypeTests.test("IteratorSequence/SequenceType") {
234234
}
235235

236236
//===----------------------------------------------------------------------===//
237-
// enumerate()
237+
// enumerated
238238
//===----------------------------------------------------------------------===//
239239

240240
// Check that the generic parameter is called 'Base'.
@@ -250,7 +250,7 @@ extension EnumeratedSequence where Base : TestProtocol1 {
250250
}
251251
}
252252

253-
SequenceTypeTests.test("enumerate") {
253+
SequenceTypeTests.test("enumerated") {
254254
typealias Element = (index: Int, element: OpaqueValue<Int>)
255255
func compareElements(lhs: Element, rhs: Element) -> Bool {
256256
return lhs.0 == rhs.0 && lhs.1.value == rhs.1.value
@@ -259,7 +259,7 @@ SequenceTypeTests.test("enumerate") {
259259
for test in enumerateTests {
260260
let s = MinimalSequence<OpaqueValue<Int>>(
261261
elements: test.sequence.map(OpaqueValue.init))
262-
var result = s.enumerate()
262+
var result = s.enumerated
263263
expectType(
264264
EnumeratedSequence<MinimalSequence<OpaqueValue<Int>>>.self,
265265
&result)

validation-test/stdlib/Slice.swift.gyb

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ SliceTests.test("Slice.subscript(_: Index)") {
7878
expectEqual(base[i].value, element.value)
7979
}
8080

81-
for (i, index) in base.indices.enumerate() {
81+
for (i, index) in base.indices.enumerated {
8282
if test.bounds.contains(i) {
8383
expectEqual(base[index].value, slice[index].value)
8484
} else {
@@ -227,7 +227,7 @@ SliceTests.test("MutableSlice.subscript(_: Index)/{get,set}") {
227227
}
228228

229229
var sliceForSetter = slice
230-
for (i, index) in base.indices.enumerate() {
230+
for (i, index) in base.indices.enumerated {
231231
if test.bounds.contains(i) {
232232
// Test getter.
233233
expectEqual(base[index].value, slice[index].value)

validation-test/stdlib/StringViews.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ tests.test("UTF8 indexes") {
613613
// Test all valid subranges si0..<si1 of positions in s. ds is
614614
// always si0.distanceTo(si1)
615615
for si0 in s.indices {
616-
for (ds, si1) in (si0..<s.endIndex).enumerate() {
616+
for (ds, si1) in (si0..<s.endIndex).enumerated {
617617

618618
// Map those unicode scalar indices into utf8 indices
619619
let u8i1 = si1.samePositionIn(u8)

0 commit comments

Comments
 (0)