Skip to content

Commit 1f88150

Browse files
author
Russ Bishop
committed
Use forEach() and fix tests
1 parent f20c925 commit 1f88150

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

stdlib/private/StdlibCollectionUnittest/CheckSequenceType.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public struct FindTest {
127127
) {
128128
self.expected = expected
129129
self.element = MinimalEquatableValue(element)
130-
self.sequence = sequence.enumerate().map {
130+
self.sequence = sequence.enumerated().map {
131131
return MinimalEquatableValue($1, identity: $0)
132132
}
133133
self.expectedLeftoverSequence = expectedLeftoverSequence.map(
@@ -1777,21 +1777,21 @@ self.test("\(testNamePrefix).forEach/semantics") {
17771777
// first()
17781778
//===----------------------------------------------------------------------===//
17791779

1780-
SequenceTypeTests.test("\(testNamePrefix).first/semantics") {
1780+
self.test("\(testNamePrefix).first/semantics") {
17811781
for test in findTests {
1782-
let s = MinimalSequence<MinimalEquatableValue>(elements: test.sequence)
1782+
let s = makeWrappedSequenceWithEquatableElement(test.sequence)
17831783
let closureLifetimeTracker = LifetimeTracked(0)
17841784
let found = s.first {
17851785
_blackHole(closureLifetimeTracker)
1786-
return $0 == test.element
1786+
return $0 == wrapValueIntoEquatable(test.element)
17871787
}
17881788
expectEqual(
1789-
test.expected == nil ? nil : test.element,
1789+
test.expected == nil ? nil : wrapValueIntoEquatable(test.element),
17901790
found,
17911791
stackTrace: SourceLocStack().with(test.loc))
1792-
if let found = found {
1792+
if test.expected != nil {
17931793
expectEqual(
1794-
test.expected, found.identity,
1794+
test.expected, (found as? MinimalEquatableValue)?.identity,
17951795
"find() should find only the first element matching its predicate")
17961796
}
17971797
}

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public struct ${Self}<
249249
where predicate: @noescape (Base.Iterator.Element) throws -> Void
250250
) rethrows -> Base.Iterator.Element? {
251251
Log.find[selfType] += 1
252-
return base.find(where: predicate)
252+
return try base.find(where: predicate)
253253
}
254254

255255
public typealias SubSequence = Base.SubSequence

stdlib/public/core/Sequence.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,13 @@ extension Sequence {
968968
try body(element)
969969
}
970970
}
971-
971+
}
972+
973+
private enum _StopIteration: ErrorProtocol {
974+
case stop
975+
}
976+
977+
extension Sequence {
972978
/// Returns the first element of the sequence that satisfies the given
973979
/// predicate or nil if no such element is found.
974980
///
@@ -979,12 +985,16 @@ extension Sequence {
979985
public func first(
980986
where predicate: @noescape (Iterator.Element) throws -> Bool
981987
) rethrows -> Iterator.Element? {
982-
for element in self {
983-
if try predicate(element) {
984-
return element
988+
var foundElement: Iterator.Element? = nil
989+
do {
990+
try self.forEach {
991+
if try predicate($0) {
992+
foundElement = $0
993+
throw _StopIteration.stop
994+
}
985995
}
986-
}
987-
return nil
996+
} catch is _StopIteration { }
997+
return foundElement
988998
}
989999
}
9901000

validation-test/stdlib/SequenceType.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ SequenceTypeTests.test("forEach/dispatch") {
950950

951951
SequenceTypeTests.test("first/dispatch") {
952952
let tester = SequenceLog.dispatchTester([OpaqueValue(1)])
953-
tester.first { return $0.value == 1 }
953+
tester.first { $0.value == 1 }
954954
expectCustomizable(tester, tester.log.first)
955955
}
956956

0 commit comments

Comments
 (0)