Skip to content

Commit

Permalink
Implement 'Collection.first_position(of:)'
Browse files Browse the repository at this point in the history
  • Loading branch information
kyouko-taiga committed Aug 5, 2024
1 parent 9338c0e commit d4eacda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions StandardLibrary/Sources/Core/Collection.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,15 @@ public extension Collection {
}

}

public extension Collection where Element: Equatable {

/// Returns the position of the first element of `self` that is equal to `needle`, or
/// `end_position()` if no such element exists.
///
/// - Complexity: O(n), where n is the number of elements in `self`.
public fun first_position(of needle: Element) -> Position {
first_position(where: fun (_ e) { e == needle })
}

}
1 change: 1 addition & 0 deletions Tests/LibraryTests/TestCases/CollectionTests.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fun test_count() {
fun test_first_position() {
let a = 4
precondition(a.first_position(where: fun (_ x) { x.copy() }) == 2)
precondition(a.first_position(of: true) == 2)
}

fun test_reduce() {
Expand Down

0 comments on commit d4eacda

Please sign in to comment.