Skip to content

Commit f20c925

Browse files
author
Russ Bishop
committed
Changes for code review
1 parent 1d8edfa commit f20c925

File tree

4 files changed

+48
-30
lines changed

4 files changed

+48
-30
lines changed

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,8 @@ public struct FindTest {
127127
) {
128128
self.expected = expected
129129
self.element = MinimalEquatableValue(element)
130-
var elementIndex = 0
131-
self.sequence = sequence.map {
132-
defer { elementIndex += 1 }
133-
return MinimalEquatableValue($0, identity: elementIndex)
130+
self.sequence = sequence.enumerate().map {
131+
return MinimalEquatableValue($1, identity: $0)
134132
}
135133
self.expectedLeftoverSequence = expectedLeftoverSequence.map(
136134
MinimalEquatableValue.init)
@@ -1775,6 +1773,30 @@ self.test("\(testNamePrefix).forEach/semantics") {
17751773
}
17761774
}
17771775

1776+
//===----------------------------------------------------------------------===//
1777+
// first()
1778+
//===----------------------------------------------------------------------===//
1779+
1780+
SequenceTypeTests.test("\(testNamePrefix).first/semantics") {
1781+
for test in findTests {
1782+
let s = MinimalSequence<MinimalEquatableValue>(elements: test.sequence)
1783+
let closureLifetimeTracker = LifetimeTracked(0)
1784+
let found = s.first {
1785+
_blackHole(closureLifetimeTracker)
1786+
return $0 == test.element
1787+
}
1788+
expectEqual(
1789+
test.expected == nil ? nil : test.element,
1790+
found,
1791+
stackTrace: SourceLocStack().with(test.loc))
1792+
if let found = found {
1793+
expectEqual(
1794+
test.expected, found.identity,
1795+
"find() should find only the first element matching its predicate")
1796+
}
1797+
}
1798+
}
1799+
17781800
//===----------------------------------------------------------------------===//
17791801
// _preprocessingPass()
17801802
//===----------------------------------------------------------------------===//

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class SequenceLog {
8181
public static var map = TypeIndexed(0)
8282
public static var filter = TypeIndexed(0)
8383
public static var forEach = TypeIndexed(0)
84+
public static var find = TypeIndexed(0)
8485
public static var dropFirst = TypeIndexed(0)
8586
public static var dropLast = TypeIndexed(0)
8687
public static var prefixMaxLength = TypeIndexed(0)
@@ -243,6 +244,13 @@ public struct ${Self}<
243244
Log.forEach[selfType] += 1
244245
try base.forEach(body)
245246
}
247+
248+
public func find(
249+
where predicate: @noescape (Base.Iterator.Element) throws -> Void
250+
) rethrows -> Base.Iterator.Element? {
251+
Log.find[selfType] += 1
252+
return base.find(where: predicate)
253+
}
246254

247255
public typealias SubSequence = Base.SubSequence
248256

stdlib/public/core/Sequence.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,12 @@ public protocol Sequence {
567567
/// Returns the first element of the sequence that satisfies the given
568568
/// predicate or nil if no such element is found.
569569
///
570-
/// - Parameter predicate: A closure that takes an element of the
570+
/// - Parameter where: A closure that takes an element of the
571571
/// sequence as its argument and returns a Boolean value indicating
572572
/// whether the element is a match.
573573
/// - Returns: The first match or `nil` if there was no match.
574-
@warn_unused_result
575574
func first(
576-
predicate: @noescape (Iterator.Element) throws -> Bool
575+
where: @noescape (Iterator.Element) throws -> Bool
577576
) rethrows -> Iterator.Element?
578577

579578
@warn_unused_result
@@ -962,7 +961,6 @@ extension Sequence {
962961
///
963962
/// - Parameter body: A closure that takes an element of the sequence as a
964963
/// parameter.
965-
@warn_unused_result
966964
public func forEach(
967965
_ body: @noescape (Iterator.Element) throws -> Void
968966
) rethrows {
@@ -974,12 +972,12 @@ extension Sequence {
974972
/// Returns the first element of the sequence that satisfies the given
975973
/// predicate or nil if no such element is found.
976974
///
977-
/// - Parameter predicate: A closure that takes an element of the
975+
/// - Parameter where: A closure that takes an element of the
978976
/// sequence as its argument and returns a Boolean value indicating
979977
/// whether the element is a match.
980978
/// - Returns: The first match or `nil` if there was no match.
981979
public func first(
982-
predicate: @noescape (Iterator.Element) throws -> Bool
980+
where predicate: @noescape (Iterator.Element) throws -> Bool
983981
) rethrows -> Iterator.Element? {
984982
for element in self {
985983
if try predicate(element) {

validation-test/stdlib/SequenceType.swift.gyb

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -616,26 +616,6 @@ SequenceTypeTests.test("contains/Predicate") {
616616
}
617617
}
618618

619-
//===----------------------------------------------------------------------===//
620-
// first()
621-
//===----------------------------------------------------------------------===//
622-
623-
SequenceTypeTests.test("first/Predicate") {
624-
for test in findTests {
625-
let s = MinimalSequence<MinimalEquatableValue>(elements: test.sequence)
626-
let found = s.first { $0 == test.element }
627-
expectEqual(
628-
test.expected == nil ? nil : test.element,
629-
found,
630-
stackTrace: SourceLocStack().with(test.loc))
631-
if let found = found {
632-
expectEqual(
633-
test.expected, found.identity,
634-
"find() should find only the first element matching its predicate")
635-
}
636-
}
637-
}
638-
639619
//===----------------------------------------------------------------------===//
640620
// reduce()
641621
//===----------------------------------------------------------------------===//
@@ -964,6 +944,16 @@ SequenceTypeTests.test("forEach/dispatch") {
964944
expectCustomizable(tester, tester.log.forEach)
965945
}
966946

947+
//===----------------------------------------------------------------------===//
948+
// first()
949+
//===----------------------------------------------------------------------===//
950+
951+
SequenceTypeTests.test("first/dispatch") {
952+
let tester = SequenceLog.dispatchTester([OpaqueValue(1)])
953+
tester.first { return $0.value == 1 }
954+
expectCustomizable(tester, tester.log.first)
955+
}
956+
967957
//===----------------------------------------------------------------------===//
968958
// dropFirst()
969959
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)