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

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

Functions

template<typename = void>
auto coveo::linq::first_or_default () -> detail::first_or_default_impl_0<>
 Returns first element in sequence, or default value. More...
 
template<typename Pred >
auto coveo::linq::first_or_default (const Pred &pred) -> detail::first_or_default_impl_1< Pred >
 Returns first element in sequence that satisfy predicate, or default value. More...
 

Detailed Description

The first_or_default operator returns the first element in a sequence, or the first 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: FirstOrDefault

Function Documentation

◆ first_or_default() [1/2]

template<typename = void>
auto coveo::linq::first_or_default ( ) -> detail::first_or_default_impl_0<>

Returns the first 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 fir1 = from(YES)
| first();
auto fir2 = from(NAY)
| first();
// fir1 == 42
// fir2 == 0
Returns
(Once applied) First element in sequence, or a default value if sequence does not have elements.

◆ first_or_default() [2/2]

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

Returns the first 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 odd = from(NUMS)
| first([](int i) { return i % 2 != 0; });
auto big = from(NUMS)
| first([](int i) { return i >= 90; });
// odd == 23
// big == 0
Parameters
predPredicate to satisfy.
Returns
(Once applied) First element in sequence for which pred returns true or, if no such element exists in sequence, a default value.