Skip to content

[stdlib][4.2] Don’t use an error for flow control in Sequence.first(where:) #17394

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
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
20 changes: 5 additions & 15 deletions stdlib/public/core/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,6 @@ extension Sequence {
}
}

@usableFromInline
@_frozen
internal enum _StopIteration : Error {
case stop
}

extension Sequence {
/// Returns the first element of the sequence that satisfies the given
/// predicate.
Expand All @@ -971,16 +965,12 @@ extension Sequence {
public func first(
where predicate: (Element) throws -> Bool
) rethrows -> Element? {
var foundElement: Element?
do {
try self.forEach {
if try predicate($0) {
foundElement = $0
throw _StopIteration.stop
}
for element in self {
if try predicate(element) {
return element
}
} catch is _StopIteration { }
return foundElement
}
return nil
}
}

Expand Down