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

Returns last element in a sequence. More...

Functions

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

Detailed Description

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

This is a terminal operator.

.NET equivalent: Last

Function Documentation

◆ last() [1/2]

template<typename = void>
auto coveo::linq::last ( ) -> detail::last_impl_0<>

Returns the last 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 las1 = from(YES)
| last();
// las1 == 66
// This throws an exception:
// auto las2 = from(NAY)
// | last();
Returns
(Once applied) Last element in sequence.
Exceptions
coveo::linq::empty_sequenceThe sequence does not have elements.

◆ last() [2/2]

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

Returns the last 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 even = from(NUMS)
| last([](int i) { return i % 2 == 0; });
// even == 66
// This throws an exception:
// auto big = from(NUMS)
// | last([](int i) { return i >= 90; });
Parameters
predPredicate to satisfy.
Returns
(Once applied) Last 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.