From d4eacda41ca790dd7a93ab17ea108a0e40808ce9 Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Mon, 5 Aug 2024 11:55:37 +0200 Subject: [PATCH] Implement 'Collection.first_position(of:)' --- StandardLibrary/Sources/Core/Collection.hylo | 12 ++++++++++++ Tests/LibraryTests/TestCases/CollectionTests.hylo | 1 + 2 files changed, 13 insertions(+) diff --git a/StandardLibrary/Sources/Core/Collection.hylo b/StandardLibrary/Sources/Core/Collection.hylo index 11c6a243b..d7d9ba30d 100644 --- a/StandardLibrary/Sources/Core/Collection.hylo +++ b/StandardLibrary/Sources/Core/Collection.hylo @@ -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 }) + } + +} diff --git a/Tests/LibraryTests/TestCases/CollectionTests.hylo b/Tests/LibraryTests/TestCases/CollectionTests.hylo index 4c8b97f3c..0b24a76e2 100644 --- a/Tests/LibraryTests/TestCases/CollectionTests.hylo +++ b/Tests/LibraryTests/TestCases/CollectionTests.hylo @@ -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() {