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

Returns first element in a sequence. More...

Functions

template<typename = void>
auto coveo::linq::first () -> detail::first_impl_0<>
 Returns first element in sequence. More...
 
template<typename Pred >
auto coveo::linq::first (const Pred &pred) -> detail::first_impl_1< Pred >
 Returns first element in sequence that satisfy predicate. More...
 

Detailed Description

The first 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, an exception is thrown.

This is a terminal operator.

.NET equivalent: First

Function Documentation

◆ first() [1/2]

template<typename = void>
auto coveo::linq::first ( ) -> detail::first_impl_0<>

Returns the first element in a sequence. If the sequence does not have elements, coveo::linq::empty_sequence is thrown.

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();
// fir1 == 42
// This throws an exception:
// auto fir2 = from(NAY)
// | first();
Returns
(Once applied) First element in sequence.
Exceptions
coveo::linq::empty_sequenceThe sequence does not have elements.

◆ first() [2/2]

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

Returns the first element in a sequence for which the given predicate returns true. If the sequence does not have elements, coveo::linq::empty_sequence is thrown; if the sequence does not contain an element that satisfy the predicate, coveo::linq::out_of_range is thrown.

Use like this:

const int NUMS[] = { 42, 23, 66 };
using namespace coveo::linq;
auto odd = from(NUMS)
| first([](int i) { return i % 2 != 0; });
// odd == 23
// This throws an exception:
// auto big = from(NUMS)
// | first([](int i) { return i >= 90; });
Parameters
predPredicate to satisfy.
Returns
(Once applied) First element in sequence for which pred returns true.
Exceptions
coveo::linq::empty_sequenceThe sequence does not have elements.
coveo::linq::out_of_rangeThe sequence has no element that satisfy pred.