diff --git a/AK/Span.h b/AK/Span.h index bff3ec5c2d0..6a1644ebdf1 100644 --- a/AK/Span.h +++ b/AK/Span.h @@ -306,6 +306,28 @@ public: { return { data(), size() }; } + + template + Optional last_matching(TUnaryPredicate const& predicate) + { + for (ssize_t i = size() - 1; i >= 0; --i) { + if (predicate(at(i))) { + return at(i); + } + } + return {}; + } + + template + Optional first_matching(TUnaryPredicate const& predicate) + { + for (size_t i = 0; i < size(); ++i) { + if (predicate(at(i))) { + return at(i); + } + } + return {}; + } }; template