Skip to content

Commit 8b9dd25

Browse files
tonyarnoldairspeedswift
authored andcommitted
Don’t use an error for flow control in Sequence.first(where:) (#17387)
1 parent 0af9976 commit 8b9dd25

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -944,12 +944,6 @@ extension Sequence {
944944
}
945945
}
946946

947-
@usableFromInline
948-
@_frozen
949-
internal enum _StopIteration : Error {
950-
case stop
951-
}
952-
953947
extension Sequence {
954948
/// Returns the first element of the sequence that satisfies the given
955949
/// predicate.
@@ -972,16 +966,12 @@ extension Sequence {
972966
public func first(
973967
where predicate: (Element) throws -> Bool
974968
) rethrows -> Element? {
975-
var foundElement: Element?
976-
do {
977-
try self.forEach {
978-
if try predicate($0) {
979-
foundElement = $0
980-
throw _StopIteration.stop
981-
}
969+
for element in self {
970+
if try predicate(element) {
971+
return element
982972
}
983-
} catch is _StopIteration { }
984-
return foundElement
973+
}
974+
return nil
985975
}
986976
}
987977

0 commit comments

Comments
 (0)