coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions

Returns last element in a sequence, or a default value. More...

Functions

template<typename = void>
auto coveo::linq::last_or_default () -> detail::last_or_default_impl_0<>
 Returns last element in sequence, or default value. More...
 
template<typename Pred >
auto coveo::linq::last_or_default (const Pred &pred) -> detail::last_or_default_impl_1< Pred >
 Returns last element in sequence that satisfy predicate, or default value. More...
 

Detailed Description

The last_or_default operator returns the last element in a sequence, or the last element to satisfy a predicate. If the sequence does not have such an element, a default value is returned.

This is a terminal operator.

.NET equivalent: LastOrDefault

Function Documentation

◆ last_or_default() [1/2]

template<typename = void>
auto coveo::linq::last_or_default ( ) -> detail::last_or_default_impl_0<>

Returns the last element in a sequence. If the sequence does not have elements, a default-initialized value is returned instead.

Use like this:

const std::vector<int> YES = { 42, 23, 66 };
const std::vector<int> NAY;
using namespace coveo::linq;
auto las1 = from(YES)
| last();
auto las2 = from(NAY)
| last();
// las1 == 66
// las2 == 0
Returns
(Once applied) Last element in sequence, or a default value if sequence does not have elements.

◆ last_or_default() [2/2]

template<typename Pred >
auto coveo::linq::last_or_default ( const Pred &  pred) -> detail::last_or_default_impl_1<Pred>

Returns the last element in a sequence for which the given predicate returns true. If the sequence does not have elements or does not contain an element that satisfy the predicate, a default-initialized value is returned instead.

Use like this:

const int NUMS[] = { 42, 23, 66 };
using namespace coveo::linq;
auto even = from(NUMS)
| last([](int i) { return i % 2 == 0; });
auto big = from(NUMS)
| last([](int i) { return i >= 90; });
// even == 66
// big == 0
Parameters
predPredicate to satisfy.
Returns
(Once applied) Last element in sequence for which pred returns true or, if no such element exists in sequence, a default value.