Add asynchronous first index function

This commit is contained in:
david-swift 2024-09-30 09:44:17 +02:00
parent 9a3760b24c
commit ba6a46f793

View File

@ -111,6 +111,21 @@ extension Array {
return nil
}
/// Returns the index of the first element of the sequence that satisfies the given predicate.
/// - Parameter predicate: A closure that takes an element of the sequence as its argument
/// and returns a Boolean value indicating whether the element is a match.
/// - Returns: The index of the first element of the sequence that satisfies `predicate`,
/// or `nil` if there is no element that satisfies `predicate`.
public func firstIndex(where predicate: (Element) async throws -> Bool) async rethrows -> Int? {
for (index, element) in enumerated() {
let matches = try await predicate(element)
if matches {
return index
}
}
return nil
}
/// Returns the last element of the sequence that satisfies the given predicate.
/// - Parameter predicate: A closure that takes an element of the sequence as its argument
/// and returns a Boolean value indicating whether the element is a match.