Skip to content

[SE-0045] Address Dmitri's comments on #3600 #4346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ Note: This is in reverse chronological order, so newer entries are added to the
Swift 3.0
---------

* [SE-0045](https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md)

The `Sequence` protocol now includes two new members, `take(while:)` and
`drop(while:)`. `take(while:)` is used to request the longest subsequence
satisfying a predicate. `drop(while:)` is used to request the subsequence
remaining after dropping the longest subsequence satisfying a predicate.

* [SE-0136](https://github.com/apple/swift-evolution/blob/master/proposals/0136-memory-layout-of-values.md) and [SE-0101](https://github.com/apple/swift-evolution/blob/master/proposals/0101-standardizing-sizeof-naming.md)

The functions `sizeof()`, `strideof()`, and `alignof()` have been removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public struct ${Self}<
}

public func drop(
while predicate: @noescape (Base.Iterator.Element) throws -> Bool
while predicate: (Base.Iterator.Element) throws -> Bool
) rethrows -> SubSequence {
Log.dropWhile[selfType] += 1
return try base.drop(while: predicate)
Expand All @@ -278,7 +278,7 @@ public struct ${Self}<
}

public func prefix(
while predicate: @noescape (Base.Iterator.Element) throws -> Bool
while predicate: (Base.Iterator.Element) throws -> Bool
) rethrows -> SubSequence {
Log.prefixWhile[selfType] += 1
return try base.prefix(while: predicate)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ extension Collection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
public func drop(
while predicate: @noescape (Iterator.Element) throws -> Bool
while predicate: (Iterator.Element) throws -> Bool
) rethrows -> SubSequence {
var start = startIndex
while try start != endIndex && predicate(self[start]) {
Expand Down Expand Up @@ -1374,7 +1374,7 @@ extension Collection {
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
public func prefix(
while predicate: @noescape (Iterator.Element) throws -> Bool
while predicate: (Iterator.Element) throws -> Bool
) rethrows -> SubSequence {
var end = startIndex
while try end != endIndex && predicate(self[end]) {
Expand Down
12 changes: 10 additions & 2 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,14 @@ internal class _PrefixSequence<Base : IteratorProtocol>
maxLength: Swift.min(maxLength, self._maxLength),
taken: _taken))
}

internal func drop(
while predicate: (Base.Element) throws -> Bool
) rethrows -> AnySequence<Base.Element> {
return try AnySequence(
_DropWhileSequence(
iterator: _iterator, nextElement: nil, predicate: predicate))
}
}

/// A sequence that lazily consumes and drops `n` elements from an underlying
Expand Down Expand Up @@ -1253,8 +1261,8 @@ extension Sequence where
///
/// - Parameter predicate: A closure that takes an element of the
/// sequence as its argument and returns `true` if the element should
/// be included or `false` if it should be excluded. Once the predicate
/// returns `false` it will not be called again.
/// be included or `false` if it should be excluded. Once the predicate
/// returns `false` it will not be called again.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
public func prefix(
Expand Down
20 changes: 0 additions & 20 deletions validation-test/stdlib/Lazy.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1295,16 +1295,6 @@ let prefixDropWhileTests: [(data: [Int], value: Int, pivot: Int)] = [
([0, 10, 20, 30, 40], 40, 4),
([0, 10, 20, 30, 40], 99, 5) ]

tests.test("LazyPrefixWhileIterator") {
let base = (1...10).lazy.prefix(while: { $0 < 4 })

var iter1 = base.makeIterator()
expectEqual(1, iter1.next())
expectEqual(2, iter1.next())
expectEqual(3, iter1.next())
expectEqual(nil, iter1.next())
}

% for Kind in 'Sequence', 'Forward', 'Bidirectional':
% Self = 'Sequence' if Kind == 'Sequence' else collectionForTraversal(Kind)
% checkKind = 'ForwardCollection' if Kind == 'Forward' else Self
Expand Down Expand Up @@ -1365,16 +1355,6 @@ tests.test("LazyPrefixWhileBidirectionalCollection/AssociatedTypes") {

//===--- LazyDropWhile ----------------------------------------------------===//

tests.test("LazyDropWhileIterator") {
let base = (1...10).lazy.drop(while: { $0 < 4 })

var iter1 = base.makeIterator()
expectEqual(4, iter1.next())
expectEqual(5, iter1.next())
expectEqual(6, iter1.next())
expectEqual(7, iter1.next())
}

% for Kind in 'Sequence', 'Forward', 'Bidirectional':
% Self = 'Sequence' if Kind == 'Sequence' else collectionForTraversal(Kind)
% checkKind = 'ForwardCollection' if Kind == 'Forward' else Self
Expand Down