AK: Add an AK::find helper to return a reference to the found value

This is often more convenient than dealing with iterators.

This commit includes a couple conversions to find_value as examples.
This commit is contained in:
Timothy Flynn 2025-04-05 09:06:51 -04:00 committed by Andreas Kling
commit 7f37a8f60f
Notes: github-actions[bot] 2025-04-06 11:46:22 +00:00
5 changed files with 34 additions and 30 deletions

View file

@ -51,3 +51,14 @@ TEST_CASE(should_return_index_to_first_predicate_matching_value_in_container)
EXPECT(4 == AK::find_index(a.begin(), a.end(), 0));
}
TEST_CASE(find_value)
{
static constexpr Array array { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
auto value = find_value(array, [](auto value) { return value > 5; });
EXPECT_EQ(value, 6);
value = find_value(array, [](auto value) { return value == 12389; });
EXPECT(!value.has_value());
}