Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Add asynchronous first index function
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Sep 30, 2024
1 parent 9a3760b commit ba6a46f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Sources/Model/Extensions/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ba6a46f

Please sign in to comment.