Skip to content

Commit f3edb42

Browse files
committed
Remove customization points from Sequence and Collection
1 parent e089f2a commit f3edb42

16 files changed

+41
-302
lines changed

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public class SequenceLog {
7777
// Sequence
7878
public static var makeIterator = TypeIndexed(0)
7979
public static var underestimatedCount = TypeIndexed(0)
80-
public static var map = TypeIndexed(0)
8180
public static var filter = TypeIndexed(0)
82-
public static var forEach = TypeIndexed(0)
8381
public static var dropFirst = TypeIndexed(0)
8482
public static var dropLast = TypeIndexed(0)
8583
public static var dropWhile = TypeIndexed(0)
@@ -107,14 +105,12 @@ public class SequenceLog {
107105
public static var isEmpty = TypeIndexed(0)
108106
public static var count = TypeIndexed(0)
109107
public static var _customIndexOfEquatableElement = TypeIndexed(0)
110-
public static var first = TypeIndexed(0)
111108
public static var advance = TypeIndexed(0)
112109
public static var advanceLimit = TypeIndexed(0)
113110
public static var distance = TypeIndexed(0)
114111
// BidirectionalCollection
115112
public static var predecessor = TypeIndexed(0)
116113
public static var formPredecessor = TypeIndexed(0)
117-
public static var last = TypeIndexed(0)
118114
// MutableCollection
119115
public static var subscriptIndexSet = TypeIndexed(0)
120116
public static var subscriptRangeSet = TypeIndexed(0)
@@ -222,25 +218,13 @@ extension LoggingSequence: Sequence {
222218
return base.underestimatedCount
223219
}
224220

225-
public func map<T>(
226-
_ transform: (Element) throws -> T
227-
) rethrows -> [T] {
228-
SequenceLog.map[selfType] += 1
229-
return try base.map(transform)
230-
}
231-
232221
public func filter(
233222
_ isIncluded: (Element) throws -> Bool
234223
) rethrows -> [Element] {
235224
SequenceLog.filter[selfType] += 1
236225
return try base.filter(isIncluded)
237226
}
238227

239-
public func forEach(_ body: (Element) throws -> Void) rethrows {
240-
SequenceLog.forEach[selfType] += 1
241-
try base.forEach(body)
242-
}
243-
244228
public func dropFirst(_ n: Int) -> SubSequence {
245229
SequenceLog.dropFirst[selfType] += 1
246230
return base.dropFirst(n)
@@ -406,11 +390,6 @@ extension LoggingCollection: Collection {
406390
return base._customIndexOfEquatableElement(element)
407391
}
408392

409-
public var first: Element? {
410-
CollectionLog.first[selfType] += 1
411-
return base.first
412-
}
413-
414393
public func index(_ i: Index, offsetBy n: Int) -> Index {
415394
CollectionLog.advance[selfType] += 1
416395
return base.index(i, offsetBy: n)
@@ -443,11 +422,6 @@ extension LoggingBidirectionalCollection: BidirectionalCollection {
443422
BidirectionalCollectionLog.formPredecessor[selfType] += 1
444423
base.formIndex(before: &i)
445424
}
446-
447-
public var last: Element? {
448-
BidirectionalCollectionLog.last[selfType] += 1
449-
return base.last
450-
}
451425
}
452426

453427
public typealias LoggingRandomAccessCollection<Base: RandomAccessCollection>

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,6 @@ where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
185185
/// // c == MyFancyCollection([2, 4, 6, 8, 10])
186186
override var indices: Indices { get }
187187

188-
// TODO: swift-3-indexing-model: tests.
189-
/// The last element of the collection.
190-
///
191-
/// If the collection is empty, the value of this property is `nil`.
192-
///
193-
/// let numbers = [10, 20, 30, 40, 50]
194-
/// if let lastNumber = numbers.last {
195-
/// print(lastNumber)
196-
/// }
197-
/// // Prints "50"
198-
///
199-
/// - Complexity: O(1)
200-
var last: Element? { get }
201-
202188
/// Accesses a contiguous subrange of the collection's elements.
203189
///
204190
/// The accessed slice uses the same indices for the same elements as the

stdlib/public/core/Collection.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,6 @@ public protocol Collection: Sequence where SubSequence: Collection {
632632
/// - Complexity: Hopefully less than O(`count`).
633633
func _customLastIndexOfEquatableElement(_ element: Element) -> Index??
634634

635-
// FIXME(move-only types): `first` might not be implementable by collections
636-
// with move-only elements, since they would need to be able to somehow form
637-
// a temporary `Optional<Element>` value from a nonoptional Element without
638-
// modifying the collection.
639-
640-
/// The first element of the collection.
641-
///
642-
/// If the collection is empty, the value of this property is `nil`.
643-
///
644-
/// let numbers = [10, 20, 30, 40, 50]
645-
/// if let firstNumber = numbers.first {
646-
/// print(firstNumber)
647-
/// }
648-
/// // Prints "10"
649-
var first: Element? { get }
650-
651635
/// Returns an index that is the specified distance from the given index.
652636
///
653637
/// The following example obtains an index advanced four positions from a
@@ -1346,8 +1330,7 @@ extension Collection {
13461330
@inlinable
13471331
public __consuming func dropFirst(_ k: Int) -> SubSequence {
13481332
_precondition(k >= 0, "Can't drop a negative number of elements from a collection")
1349-
let start = index(startIndex,
1350-
offsetBy: k, limitedBy: endIndex) ?? endIndex
1333+
let start = index(startIndex, offsetBy: k, limitedBy: endIndex) ?? endIndex
13511334
return self[start..<endIndex]
13521335
}
13531336

stdlib/public/core/Dictionary.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -728,19 +728,6 @@ extension Dictionary: Collection {
728728
public var isEmpty: Bool {
729729
return count == 0
730730
}
731-
732-
/// The first element of the dictionary.
733-
///
734-
/// The first element of the dictionary is not necessarily the first element
735-
/// added to the dictionary. Don't expect any particular ordering of
736-
/// dictionary elements.
737-
///
738-
/// If the dictionary is empty, the value of this property is `nil`.
739-
@inlinable
740-
public var first: Element? {
741-
var it = makeIterator()
742-
return it.next()
743-
}
744731
}
745732

746733
extension Dictionary {

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,6 @@ internal class _AnyRandomAccessCollectionBox<Element>
352352
func _customLastIndexOfEquatableElement(element: Element) -> Index??
353353
*/
354354

355-
@inlinable // FIXME(sil-serialize-all)
356-
internal var _first: Element? { _abstract() }
357-
358355
@inlinable
359356
internal init(
360357
_startIndex: _AnyIndexBox,
@@ -385,8 +382,6 @@ internal class _AnyRandomAccessCollectionBox<Element>
385382
internal func _index(before i: _AnyIndexBox) -> _AnyIndexBox { _abstract() }
386383
@inlinable
387384
internal func _formIndex(before i: _AnyIndexBox) { _abstract() }
388-
@inlinable
389-
internal var _last: Element? { _abstract() }
390385
% end
391386
}
392387

@@ -617,11 +612,6 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>
617612
return numericCast(_base.count)
618613
}
619614

620-
@inlinable
621-
internal override var _first: Element? {
622-
return _base.first
623-
}
624-
625615
% if Kind in ['BidirectionalCollection', 'RandomAccessCollection']:
626616
@inlinable
627617
internal override func _index(before position: _AnyIndexBox) -> _AnyIndexBox {
@@ -636,10 +626,6 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>
636626
fatalError("Index type mismatch!")
637627
}
638628

639-
@inlinable
640-
internal override var _last: Element? {
641-
return _base.last
642-
}
643629
% end
644630

645631
% end
@@ -1118,11 +1104,6 @@ extension ${Self}: ${SelfProtocol} {
11181104
return _box._count
11191105
}
11201106

1121-
@inlinable
1122-
public var first: Element? {
1123-
return _box._first
1124-
}
1125-
11261107
% if Traversal == 'Bidirectional' or Traversal == 'RandomAccess':
11271108
@inlinable
11281109
public func index(before i: Index) -> Index {
@@ -1138,11 +1119,6 @@ extension ${Self}: ${SelfProtocol} {
11381119
i = index(before: i)
11391120
}
11401121
}
1141-
1142-
@inlinable
1143-
public var last: Element? {
1144-
return _box._last
1145-
}
11461122
% end
11471123
}
11481124

stdlib/public/core/LazyCollection.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ extension LazyCollection : Collection {
201201
return _base._customLastIndexOfEquatableElement(element)
202202
}
203203

204-
/// Returns the first element of `self`, or `nil` if `self` is empty.
205-
@inlinable
206-
public var first: Element? {
207-
return _base.first
208-
}
209-
210204
// TODO: swift-3-indexing-model - add docs
211205
@inlinable
212206
public func index(_ i: Index, offsetBy n: Int) -> Index {
@@ -235,11 +229,6 @@ extension LazyCollection : BidirectionalCollection
235229
public func index(before i: Index) -> Index {
236230
return _base.index(before: i)
237231
}
238-
239-
@inlinable
240-
public var last: Element? {
241-
return _base.last
242-
}
243232
}
244233

245234
extension LazyCollection : RandomAccessCollection

stdlib/public/core/Map.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ extension LazyMapCollection: LazyCollectionProtocol {
187187
return _base.count
188188
}
189189

190-
@inlinable
191-
public var first: Element? { return _base.first.map(_transform) }
192-
193190
@inlinable
194191
public func index(_ i: Index, offsetBy n: Int) -> Index {
195192
return _base.index(i, offsetBy: n)
@@ -223,9 +220,6 @@ extension LazyMapCollection : BidirectionalCollection
223220
public func formIndex(before i: inout Index) {
224221
_base.formIndex(before: &i)
225222
}
226-
227-
@inlinable
228-
public var last: Element? { return _base.last.map(_transform) }
229223
}
230224

231225
extension LazyMapCollection : RandomAccessCollection

stdlib/public/core/Sequence.swift

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -348,29 +348,6 @@ public protocol Sequence {
348348
/// In this case, see the documentation of `Collection.underestimatedCount`.
349349
var underestimatedCount: Int { get }
350350

351-
/// Returns an array containing the results of mapping the given closure
352-
/// over the sequence's elements.
353-
///
354-
/// In this example, `map` is used first to convert the names in the array
355-
/// to lowercase strings and then to count their characters.
356-
///
357-
/// let cast = ["Vivien", "Marlon", "Kim", "Karl"]
358-
/// let lowercaseNames = cast.map { $0.lowercased() }
359-
/// // 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
360-
/// let letterCounts = cast.map { $0.count }
361-
/// // 'letterCounts' == [6, 6, 3, 4]
362-
///
363-
/// - Parameter transform: A mapping closure. `transform` accepts an
364-
/// element of this sequence as its parameter and returns a transformed
365-
/// value of the same or of a different type.
366-
/// - Returns: An array containing the transformed elements of this
367-
/// sequence.
368-
///
369-
/// - Complexity: O(*n*), where *n* is the length of the sequence.
370-
func map<T>(
371-
_ transform: (Element) throws -> T
372-
) rethrows -> [T]
373-
374351
/// Returns an array containing, in order, the elements of the sequence
375352
/// that satisfy the given predicate.
376353
///
@@ -392,37 +369,6 @@ public protocol Sequence {
392369
_ isIncluded: (Element) throws -> Bool
393370
) rethrows -> [Element]
394371

395-
/// Calls the given closure on each element in the sequence in the same order
396-
/// as a `for`-`in` loop.
397-
///
398-
/// The two loops in the following example produce the same output:
399-
///
400-
/// let numberWords = ["one", "two", "three"]
401-
/// for word in numberWords {
402-
/// print(word)
403-
/// }
404-
/// // Prints "one"
405-
/// // Prints "two"
406-
/// // Prints "three"
407-
///
408-
/// numberWords.forEach { word in
409-
/// print(word)
410-
/// }
411-
/// // Same as above
412-
///
413-
/// Using the `forEach` method is distinct from a `for`-`in` loop in two
414-
/// important ways:
415-
///
416-
/// 1. You cannot use a `break` or `continue` statement to exit the current
417-
/// call of the `body` closure or skip subsequent calls.
418-
/// 2. Using the `return` statement in the `body` closure will exit only from
419-
/// the current call to `body`, not from any outer scope, and won't skip
420-
/// subsequent calls.
421-
///
422-
/// - Parameter body: A closure that takes an element of the sequence as a
423-
/// parameter.
424-
func forEach(_ body: (Element) throws -> Void) rethrows
425-
426372
// Note: The complexity of Sequence.dropFirst(_:) requirement
427373
// is documented as O(n) because Collection.dropFirst(_:) is
428374
// implemented in O(n), even though the default

stdlib/public/core/Set.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -391,18 +391,6 @@ extension Set: Collection {
391391
public var isEmpty: Bool {
392392
return count == 0
393393
}
394-
395-
/// The first element of the set.
396-
///
397-
/// The first element of the set is not necessarily the first element added
398-
/// to the set. Don't expect any particular ordering of set elements.
399-
///
400-
/// If the set is empty, the value of this property is `nil`.
401-
@inlinable
402-
public var first: Element? {
403-
var iterator = makeIterator()
404-
return iterator.next()
405-
}
406394
}
407395

408396
// FIXME: rdar://problem/23549059 (Optimize == for Set)

test/api-digester/Outputs/stability-stdlib-abi.swift.expected

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,33 @@ Struct _PrefixSequence has generic signature change from <τ_0_0 where τ_0_0 :
1212
/* Removed Decls */
1313
Constructor _DropFirstSequence.init(_iterator:limit:dropped:) has been removed
1414
Constructor _PrefixSequence.init(_iterator:maxLength:taken:) has been removed
15+
Func Sequence.forEach(_:) has been removed
16+
Func Sequence.map(_:) has been removed
1517
Func _DropFirstSequence.next() has been removed
1618
Func _PrefixSequence.next() has been removed
19+
Var AnyBidirectionalCollection.first has been removed
20+
Var AnyBidirectionalCollection.last has been removed
21+
Var AnyCollection.first has been removed
22+
Var AnyRandomAccessCollection.first has been removed
23+
Var AnyRandomAccessCollection.last has been removed
24+
Var BidirectionalCollection.last has been removed
25+
Var Dictionary.first has been removed
26+
Var LazyCollection.first has been removed
27+
Var LazyCollection.last has been removed
28+
Var LazyMapCollection.first has been removed
29+
Var LazyMapCollection.last has been removed
30+
Var Set.first has been removed
31+
Var _AnyBidirectionalCollectionBox._last has been removed
32+
Var _AnyCollectionBox._first has been removed
33+
Var _BidirectionalCollectionBox._first has been removed
34+
Var _BidirectionalCollectionBox._last has been removed
35+
Var _CollectionBox._first has been removed
1736
Var _DropFirstSequence._dropped has been removed
1837
Var _DropFirstSequence._iterator has been removed
1938
Var _PrefixSequence._iterator has been removed
2039
Var _PrefixSequence._taken has been removed
40+
Var _RandomAccessCollectionBox._first has been removed
41+
Var _RandomAccessCollectionBox._last has been removed
2142

2243
/* Moved Decls */
2344
Struct _DropFirstSequence has been renamed to Struct DropFirstSequence

0 commit comments

Comments
 (0)