coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions
select / select_with_index / select_many / select_many_with_index

Projects elements in a sequence into another form. More...

Functions

template<typename Selector >
auto coveo::linq::select (Selector &&sel) -> detail::select_impl< detail::indexless_selector_proxy< Selector >>
 Projects elements in sequence into another form. More...
 
template<typename Selector >
auto coveo::linq::select_with_index (Selector &&sel) -> detail::select_impl< Selector >
 Projects elements in sequence into another form using element index. More...
 
template<typename Selector >
auto coveo::linq::select_many (Selector &&sel) -> detail::select_many_impl< detail::indexless_selector_proxy< Selector >>
 Projects elements in sequence into many values and flattens them. More...
 
template<typename Selector >
auto coveo::linq::select_many_with_index (Selector &&sel) -> detail::select_many_impl< Selector >
 Projects elements in sequence into many values using element index and flattens them. More...
 

Detailed Description

The select operator scans a sequence and projects each of its element into another form using a selector, returning a sequence of the results. A little similar to std::transform().

.NET equivalent: Select / SelectMany

Function Documentation

◆ select()

template<typename Selector >
auto coveo::linq::select ( Selector &&  sel) -> detail::select_impl<detail::indexless_selector_proxy<Selector>>

Produces a new sequence by projecting each element in the source sequence into another form, a little like std::transform().

Use like this:

const std::vector<std::string> STRS = {
"Life, the Universe and Everything",
"Hangar",
"Route",
};
using namespace coveo::linq;
auto seq = from(STRS)
| select([](const std::string& s) { return s.size(); });
// seq == { 33, 6, 5 }
Parameters
selSelector used to project each element in source sequence into another form.
Returns
(Once applied) Sequence of projected values.

◆ select_with_index()

template<typename Selector >
auto coveo::linq::select_with_index ( Selector &&  sel) -> detail::select_impl<Selector>

Produces a new sequence by projecting each element in the source sequence into another form, a little like std::transform(). The selector receives, as second argument, the index of the element in the source sequence.

Use like this:

const std::vector<std::string> STRS = {
"Life, the Universe and Everything",
"Hangar",
"Route",
};
using namespace coveo::linq;
auto seq = from(STRS)
| select_with_index([](const std::string& s, std::size_t i) { return s.size() * (i + 1); });
// seq == { 33, 12, 15 }
Parameters
selSelector used to project each element in source sequence into another form. Receives index of element as second argument.
Returns
(Once applied) Sequence of projected values.

◆ select_many()

template<typename Selector >
auto coveo::linq::select_many ( Selector &&  sel) -> detail::select_many_impl<detail::indexless_selector_proxy<Selector>>

Produces a new sequence by projecting each element in the source sequence into a sequence of new values, then flattens all those sequences.

Use like this:

const std::vector<std::string> STRS = {
"Life, the Universe and Everything",
"Hangar",
"Route",
};
using namespace coveo::linq;
auto seq = from(STRS)
| select_many([](const std::string& s) {
return std::vector<std::size_t>{
s.size(),
static_cast<std::size_t>(s.front() - 'A'),
};
});
// seq == { 33, 11, 6, 7, 5, 17 }
Parameters
selSelector used to project each element in source sequence into a sequence of values.
Returns
(Once applied) Sequence of flattened projected values.

◆ select_many_with_index()

template<typename Selector >
auto coveo::linq::select_many_with_index ( Selector &&  sel) -> detail::select_many_impl<Selector>

Produces a new sequence by projecting each element in the source sequence into a sequence of new values, then flattens all those sequences. The selector receives, as second argument, the index of the element in the source sequence.

Use like this:

const std::vector<std::string> STRS = {
"Life, the Universe and Everything",
"Hangar",
"Route",
};
using namespace coveo::linq;
auto seq = from(STRS)
| select_many_with_index([](const std::string& s, std::size_t i) {
return std::vector<std::size_t>{
s.size(),
static_cast<std::size_t>(s.front() - 'A'),
i + 1,
};
});
// seq == { 33, 11, 1, 6, 7, 2, 5, 17, 3 }
Parameters
selSelector used to project each element in source sequence into a sequence of values. Receives index of element as second argument.
Returns
(Once applied) Sequence of flattened projected values.