Skip to content

Commit a7ab3d5

Browse files
authored
Merge pull request #3177 from natecook1000/nc-docs-split
[stdlib] Fix and clean up 'split' documentation
2 parents 3ea89df + 4aaa31e commit a7ab3d5

File tree

2 files changed

+70
-53
lines changed

2 files changed

+70
-53
lines changed

stdlib/public/core/Collection.swift

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,6 @@ extension Collection where SubSequence == Slice<Self> {
11401140
}
11411141
}
11421142

1143-
// TODO: swift-3-indexing-model - review the following
11441143
extension Collection where SubSequence == Self {
11451144
/// Removes and returns the first element of the collection.
11461145
///
@@ -1149,6 +1148,7 @@ extension Collection where SubSequence == Self {
11491148
///
11501149
/// - Complexity: O(1)
11511150
public mutating func popFirst() -> Iterator.Element? {
1151+
// TODO: swift-3-indexing-model - review the following
11521152
guard !isEmpty else { return nil }
11531153
let element = first!
11541154
self = self[index(after: startIndex)..<endIndex]
@@ -1196,21 +1196,22 @@ extension Collection {
11961196
var i = makeIterator()
11971197
return i.next()
11981198
}
1199-
// TODO: swift-3-indexing-model - uncomment and replace above ready (or should we still use the iterator one?)
1199+
1200+
// TODO: swift-3-indexing-model - uncomment and replace above ready (or should we still use the iterator one?)
12001201
/// Returns the first element of `self`, or `nil` if `self` is empty.
12011202
///
12021203
/// - Complexity: O(1)
12031204
// public var first: Iterator.Element? {
12041205
// return isEmpty ? nil : self[startIndex]
12051206
// }
12061207

1207-
// TODO: swift-3-indexing-model - review the following
12081208
/// A value less than or equal to the number of elements in the collection.
12091209
///
12101210
/// - Complexity: O(1) if the collection conforms to
12111211
/// `RandomAccessCollection`; otherwise, O(*n*), where *n* is the length
12121212
/// of the collection.
12131213
public var underestimatedCount: Int {
1214+
// TODO: swift-3-indexing-model - review the following
12141215
return numericCast(count)
12151216
}
12161217

@@ -1223,7 +1224,7 @@ extension Collection {
12231224
return distance(from: startIndex, to: endIndex)
12241225
}
12251226

1226-
// TODO: swift-3-indexing-model - rename the following to _customIndexOfEquatable(element)?
1227+
// TODO: swift-3-indexing-model - rename the following to _customIndexOfEquatable(element)?
12271228
/// Customization point for `Collection.index(of:)`.
12281229
///
12291230
/// Define this method if the collection can find an element in less than
@@ -1245,7 +1246,6 @@ extension Collection {
12451246
//===----------------------------------------------------------------------===//
12461247

12471248
extension Collection {
1248-
// TODO: swift-3-indexing-model - review the following
12491249
/// Returns an array containing the results of mapping the given closure
12501250
/// over the sequence's elements.
12511251
///
@@ -1266,6 +1266,7 @@ extension Collection {
12661266
public func map<T>(
12671267
_ transform: @noescape (Iterator.Element) throws -> T
12681268
) rethrows -> [T] {
1269+
// TODO: swift-3-indexing-model - review the following
12691270
let count: Int = numericCast(self.count)
12701271
if count == 0 {
12711272
return []
@@ -1470,10 +1471,12 @@ extension Collection {
14701471
return prefix(upTo: index(after: position))
14711472
}
14721473

1473-
// TODO: swift-3-indexing-model - review the following
1474-
/// Returns the longest possible subsequences of the sequence, in order, that
1475-
/// don't contain elements satisfying the given predicate. Elements that are
1476-
/// used to split the sequence are not returned as part of any subsequence.
1474+
/// Returns the longest possible subsequences of the collection, in order,
1475+
/// that don't contain elements satisfying the given predicate.
1476+
///
1477+
/// The resulting array consists of at most `maxSplits + 1` subsequences.
1478+
/// Elements that are used to split the sequence are not returned as part of
1479+
/// any subsequence.
14771480
///
14781481
/// The following examples show the effects of the `maxSplits` and
14791482
/// `omittingEmptySubsequences` parameters when splitting a string using a
@@ -1483,7 +1486,7 @@ extension Collection {
14831486
/// let line = "BLANCHE: I don't want realism. I want magic!"
14841487
/// print(line.characters.split(isSeparator: { $0 == " " })
14851488
/// .map(String.init))
1486-
/// // Prints "["BLANCHE:", "I", "don't", "want", "realism.", "I", "want", "magic!"]"
1489+
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
14871490
///
14881491
/// The second example passes `1` for the `maxSplits` parameter, so the
14891492
/// original string is split just once, into two new strings.
@@ -1501,24 +1504,28 @@ extension Collection {
15011504
/// // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
15021505
///
15031506
/// - Parameters:
1504-
/// - maxSplits: The maximum number of times to split the sequence, or one
1505-
/// less than the number of subsequences to return. If `maxSplits + 1`
1506-
/// subsequences are returned, the last one is a suffix of the original
1507-
/// sequence containing the remaining elements. `maxSplits` must be
1508-
/// greater than or equal to zero.
1507+
/// - maxSplits: The maximum number of times to split the collection, or
1508+
/// one less than the number of subsequences to return. If
1509+
/// `maxSplits + 1` subsequences are returned, the last one is a suffix
1510+
/// of the original collection containing the remaining elements.
1511+
/// `maxSplits` must be greater than or equal to zero. The default value
1512+
/// is `Int.max`.
15091513
/// - omittingEmptySubsequences: If `false`, an empty subsequence is
15101514
/// returned in the result for each pair of consecutive elements
15111515
/// satisfying the `isSeparator` predicate and for each element at the
1512-
/// start or end of the sequence satisfying the `isSeparator` predicate.
1516+
/// start or end of the collection satisfying the `isSeparator`
1517+
/// predicate. The default value is `true`.
15131518
/// - isSeparator: A closure that takes an element as an argument and
1514-
/// returns a Boolean value indicating whether the sequence should be
1519+
/// returns a Boolean value indicating whether the collection should be
15151520
/// split at that element.
1516-
/// - Returns: An array of subsequences, split from this sequence's elements.
1521+
/// - Returns: An array of subsequences, split from this collection's
1522+
/// elements.
15171523
public func split(
15181524
maxSplits: Int = Int.max,
15191525
omittingEmptySubsequences: Bool = true,
15201526
isSeparator: @noescape (Iterator.Element) throws -> Bool
15211527
) rethrows -> [SubSequence] {
1528+
// TODO: swift-3-indexing-model - review the following
15221529
_precondition(maxSplits >= 0, "Must take zero or more splits")
15231530

15241531
var result: [SubSequence] = []
@@ -1560,64 +1567,67 @@ extension Collection {
15601567
}
15611568
}
15621569

1563-
// TODO: swift-3-indexing-model - review the following
15641570
extension Collection where Iterator.Element : Equatable {
1565-
/// Returns the longest possible subsequences of the sequence, in order, that
1566-
/// don't contain elements satisfying the given predicate. Elements that are
1567-
/// used to split the sequence are not returned as part of any subsequence.
1571+
/// Returns the longest possible subsequences of the collection, in order,
1572+
/// around elements equal to the given element.
1573+
///
1574+
/// The resulting array consists of at most `maxSplits + 1` subsequences.
1575+
/// Elements that are used to split the collection are not returned as part
1576+
/// of any subsequence.
15681577
///
15691578
/// The following examples show the effects of the `maxSplits` and
1570-
/// `omittingEmptySubsequences` parameters when splitting a string using a
1571-
/// closure that matches spaces. The first use of `split` returns each word
1572-
/// that was originally separated by one or more spaces.
1579+
/// `omittingEmptySubsequences` parameters when splitting a string at each
1580+
/// space character (" "). The first use of `split` returns each word that
1581+
/// was originally separated by one or more spaces.
15731582
///
15741583
/// let line = "BLANCHE: I don't want realism. I want magic!"
1575-
/// print(line.characters.split(isSeparator: { $0 == " " })
1584+
/// print(line.characters.split(separator: " ")
15761585
/// .map(String.init))
1577-
/// // Prints "["BLANCHE:", "I", "don't", "want", "realism.", "I", "want", "magic!"]"
1586+
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
15781587
///
15791588
/// The second example passes `1` for the `maxSplits` parameter, so the
15801589
/// original string is split just once, into two new strings.
15811590
///
1582-
/// print(line.characters.split(maxSplits: 1, isSeparator: { $0 == " " })
1591+
/// print(line.characters.split(separator: " ", maxSplits: 1)
15831592
/// .map(String.init))
15841593
/// // Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
15851594
///
15861595
/// The final example passes `false` for the `omittingEmptySubsequences`
15871596
/// parameter, so the returned array contains empty strings where spaces
15881597
/// were repeated.
15891598
///
1590-
/// print(line.characters.split(omittingEmptySubsequences: false, isSeparator: { $0 == " " })
1599+
/// print(line.characters.split(separator: " ", omittingEmptySubsequences: false)
15911600
/// .map(String.init))
15921601
/// // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
15931602
///
15941603
/// - Parameters:
1595-
/// - maxSplits: The maximum number of times to split the sequence, or one
1596-
/// less than the number of subsequences to return. If `maxSplits + 1`
1597-
/// subsequences are returned, the last one is a suffix of the original
1598-
/// sequence containing the remaining elements. `maxSplits` must be
1599-
/// greater than or equal to zero.
1604+
/// - separator: The element that should be split upon.
1605+
/// - maxSplits: The maximum number of times to split the collection, or
1606+
/// one less than the number of subsequences to return. If
1607+
/// `maxSplits + 1` subsequences are returned, the last one is a suffix
1608+
/// of the original collection containing the remaining elements.
1609+
/// `maxSplits` must be greater than or equal to zero. The default value
1610+
/// is `Int.max`.
16001611
/// - omittingEmptySubsequences: If `false`, an empty subsequence is
1601-
/// returned in the result for each pair of consecutive elements
1602-
/// satisfying the `isSeparator` predicate and for each element at the
1603-
/// start or end of the sequence satisfying the `isSeparator` predicate.
1604-
/// - isSeparator: A closure that takes an element as an argument and
1605-
/// returns a Boolean value indicating whether the sequence should be
1606-
/// split at that element.
1607-
/// - Returns: An array of subsequences, split from this sequence's elements.
1612+
/// returned in the result for each consecutive pair of `separator`
1613+
/// elements in the collection and for each instance of `separator` at
1614+
/// the start or end of the collection. If `true`, only nonempty
1615+
/// subsequences are returned. The default value is `true`.
1616+
/// - Returns: An array of subsequences, split from this collection's
1617+
/// elements.
16081618
public func split(
16091619
separator: Iterator.Element,
16101620
maxSplits: Int = Int.max,
16111621
omittingEmptySubsequences: Bool = true
16121622
) -> [SubSequence] {
1613-
return split(
1614-
maxSplits: maxSplits,
1615-
omittingEmptySubsequences: omittingEmptySubsequences,
1616-
isSeparator: { $0 == separator })
1623+
// TODO: swift-3-indexing-model - review the following
1624+
return split(
1625+
maxSplits: maxSplits,
1626+
omittingEmptySubsequences: omittingEmptySubsequences,
1627+
isSeparator: { $0 == separator })
16171628
}
16181629
}
16191630

1620-
// TODO: swift-3-indexing-model - review the following
16211631
extension Collection where SubSequence == Self {
16221632
/// Removes and returns the first element of the collection.
16231633
///
@@ -1629,6 +1639,7 @@ extension Collection where SubSequence == Self {
16291639
/// - SeeAlso: `popFirst()`
16301640
@discardableResult
16311641
public mutating func removeFirst() -> Iterator.Element {
1642+
// TODO: swift-3-indexing-model - review the following
16321643
_precondition(!isEmpty, "can't remove items from an empty collection")
16331644
let element = first!
16341645
self = self[index(after: startIndex)..<endIndex]

stdlib/public/core/Sequence.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,11 @@ public protocol Sequence {
510510
func suffix(_ maxLength: Int) -> SubSequence
511511

512512
/// Returns the longest possible subsequences of the sequence, in order, that
513-
/// don't contain elements satisfying the given predicate. Elements that are
514-
/// used to split the sequence are not returned as part of any subsequence.
513+
/// don't contain elements satisfying the given predicate.
514+
///
515+
/// The resulting array consists of at most `maxSplits + 1` subsequences.
516+
/// Elements that are used to split the sequence are not returned as part of
517+
/// any subsequence.
515518
///
516519
/// The following examples show the effects of the `maxSplits` and
517520
/// `omittingEmptySubsequences` parameters when splitting a string using a
@@ -521,7 +524,7 @@ public protocol Sequence {
521524
/// let line = "BLANCHE: I don't want realism. I want magic!"
522525
/// print(line.characters.split(isSeparator: { $0 == " " })
523526
/// .map(String.init))
524-
/// // Prints "["BLANCHE:", "I", "don't", "want", "realism.", "I", "want", "magic!"]"
527+
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
525528
///
526529
/// The second example passes `1` for the `maxSplits` parameter, so the
527530
/// original string is split just once, into two new strings.
@@ -822,7 +825,7 @@ extension Sequence {
822825
/// let line = "BLANCHE: I don't want realism. I want magic!"
823826
/// print(line.characters.split(isSeparator: { $0 == " " })
824827
/// .map(String.init))
825-
/// // Prints "["BLANCHE:", "I", "don't", "want", "realism.", "I", "want", "magic!"]"
828+
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
826829
///
827830
/// The second example passes `1` for the `maxSplits` parameter, so the
828831
/// original string is split just once, into two new strings.
@@ -988,8 +991,11 @@ extension Sequence {
988991

989992
extension Sequence where Iterator.Element : Equatable {
990993
/// Returns the longest possible subsequences of the sequence, in order,
991-
/// around elements equal to the given element. Elements that are used to
992-
/// split the sequence are not returned as part of any subsequence.
994+
/// around elements equal to the given element.
995+
///
996+
/// The resulting array consists of at most `maxSplits + 1` subsequences.
997+
/// Elements that are used to split the sequence are not returned as part of
998+
/// any subsequence.
993999
///
9941000
/// The following examples show the effects of the `maxSplits` and
9951001
/// `omittingEmptySubsequences` parameters when splitting a string at each
@@ -999,7 +1005,7 @@ extension Sequence where Iterator.Element : Equatable {
9991005
/// let line = "BLANCHE: I don't want realism. I want magic!"
10001006
/// print(line.characters.split(separator: " ")
10011007
/// .map(String.init))
1002-
/// // Prints "["BLANCHE:", "I", "don't", "want", "realism.", "I", "want", "magic!"]"
1008+
/// // Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
10031009
///
10041010
/// The second example passes `1` for the `maxSplits` parameter, so the
10051011
/// original string is split just once, into two new strings.

0 commit comments

Comments
 (0)