Skip to content

Commit 6e2ef88

Browse files
Don’t use an error for flow control in Sequence.first(where:) (#17387) (#17394)
1 parent e219249 commit 6e2ef88

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
@@ -943,12 +943,6 @@ extension Sequence {
943943
}
944944
}
945945

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

0 commit comments

Comments
 (0)