coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions
take / take_while / take_while_with_index

Keeps only the first elements in a sequence. More...

Functions

template<typename = void>
auto coveo::linq::take (std::size_t n) -> detail::take_impl< detail::skip_n_pred<>>
 Keeps the first N elements in sequence. More...
 
template<typename Pred >
auto coveo::linq::take_while (Pred &&pred) -> detail::take_impl< detail::indexless_selector_proxy< Pred >>
 Keeps the first elements in sequence satisfying predicate. More...
 
template<typename Pred >
auto coveo::linq::take_while_with_index (Pred &&pred) -> detail::take_impl< Pred >
 Keeps the first elements in sequence satisfying predicate using element index. More...
 

Detailed Description

The take operator (and its siblings) keep only the first elements in a sequence, either by taking a certain number or using a predicate.

.NET equivalent: Take / TakeWhile

Function Documentation

◆ take()

template<typename = void>
auto coveo::linq::take ( std::size_t  n) -> detail::take_impl<detail::skip_n_pred<>>

Given a source sequence, returns a new sequence that contains only the first n elements of the source sequence. If the source sequence contains less than n elements, returns as many elements as possible.

Use like this:

const int NUMS = { 42, 23, 66, 11, 7 };
using namespace coveo::linq;
auto seq = from(NUMS)
| take(3);
// seq == { 42, 23, 66 }
Parameters
nNumber of elements to take.
Returns
(Once applied) New sequence containing the first n elements of source sequence.

◆ take_while()

template<typename Pred >
auto coveo::linq::take_while ( Pred &&  pred) -> detail::take_impl<detail::indexless_selector_proxy<Pred>>

Given a source sequence, returns a new sequence that contains only the first elements of the source sequence that satisfy the provided predicate. If the source sequence contains no element that satisfy the predicate, returns an empty sequence.

Use like this:

const int NUMS = { 42, 23, 66, 11, 7 };
using namespace coveo::linq;
auto seq = from(NUMS)
| take_while([](int i) { return i > 20; });
// seq == { 42, 23, 66 }
Parameters
predPredicate used to take elements.
Returns
(Once applied) New sequence containing the first elements of source sequence satisfying pred.

◆ take_while_with_index()

template<typename Pred >
auto coveo::linq::take_while_with_index ( Pred &&  pred) -> detail::take_impl<Pred>

Given a source sequence, returns a new sequence that contains only the first elements of the source sequence that satisfy the provided predicate. If the source sequence contains no element that satisfy the predicate, returns an empty sequence.

The predicate is called with two parameters every time: an element from the source sequence, and its position in the sequence.

Use like this:

const int NUMS = { 42, 23, 66, 11, 7 };
using namespace coveo::linq;
auto seq = from(NUMS)
| take_while_with_index([](int i, std::size_t idx) { return i > 20 || idx < 4; });
// seq == { 42, 23, 66, 11 }
Parameters
predPredicate used to take elements.
Returns
(Once applied) New sequence containing the first elements of source sequence satisfying pred.