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

Joins and groups elements in two sequences according to their keys. More...

Functions

template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector >
auto coveo::linq::group_join (InnerSeq &&inner_seq, OuterKeySelector &&outer_key_sel, InnerKeySelector &&inner_key_sel, ResultSelector &&result_sel) -> detail::group_join_impl< InnerSeq, OuterKeySelector, InnerKeySelector, ResultSelector, detail::less<>>
 Joins and groups elements in two sequences according to their keys. More...
 
template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector , typename Pred >
auto coveo::linq::group_join (InnerSeq &&inner_seq, OuterKeySelector &&outer_key_sel, InnerKeySelector &&inner_key_sel, ResultSelector &&result_sel, Pred &&pred) -> detail::group_join_impl< InnerSeq, OuterKeySelector, InnerKeySelector, ResultSelector, Pred >
 Joins and groups elements in two sequences according to their keys using predicate. More...
 

Detailed Description

The group_join operator scans two sequences: an outer sequence and an inner sequence. For each element in the sequences, it extracts a key using some key selectors. Then, it creates groups of elements from the inner sequence and joins them to elements in the outer sequence with matching keys. Finally, a result selector is used to fold the groups into the final results.

.NET equivalent: GroupJoin

Function Documentation

◆ group_join() [1/2]

template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector >
auto coveo::linq::group_join ( InnerSeq &&  inner_seq,
OuterKeySelector &&  outer_key_sel,
InnerKeySelector &&  inner_key_sel,
ResultSelector &&  result_sel 
) -> detail::group_join_impl<InnerSeq, OuterKeySelector, InnerKeySelector, ResultSelector, detail::less<>>

Extracts keys for the elements of an outer sequence (the one on which this operator is applied, e.g. the one passed to coveo::linq::from()) and the provided inner sequence using the provided key selectors. Next, creates groups of elements from the inner sequence with keys matching that of the elements in the outer sequence. Finally, the provided result selector is used to convert the groups into the final results. The result selector is called with two arguments: an element from the outer sequence and a group of elements from the inner sequence that share the same key.

In order to match the keys, they are compared using operator<().

Use like this:

using person_data = std::pair<int, std::string>;
using person_messages = std::pair<int, std::string>;
const std::vector<person_data> PERSONS = {
{ 1, "John Doe" },
{ 2, "Jane Smith" },
};
const std::vector<person_messages> MESSAGES = {
{ 1, "This is a test message" },
{ 2, "Hello Jane!" },
{ 1, "Doctors hate him!" },
{ 1, "Welcome to the company" },
{ 2, "Todo before we leave for vacation" },
};
using namespace coveo::linq;
auto seq = from(PERSONS)
| group_join(MESSAGES,
[](const person_data& pd) { return pd.first; },
[](const person_messages& pm) { return pm.first; }
[](const person_data& pd, const coveo::enumerable<const person_messages>& e) {
std::string res = pd.second + ": ";
for (auto&& pm : e) {
res += pm.second + ", ";
}
return res;
});
// seq == {
// "John Doe: This is a test message, Doctors hate him!, Welcome to the company, ",
// "Jane Smith: Hello Jane!, Todo before we leave for vacation, "
// }
Parameters
inner_seqInner sequence to scan to create groups.
outer_key_selSelector to get keys for elements in the outer sequence.
inner_key_selSelector to get keys for elements in the inner sequence.
result_selResult selector used to produce final results.
Returns
(Once applied) Sequence of values returned by result_sel.

◆ group_join() [2/2]

template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector , typename Pred >
auto coveo::linq::group_join ( InnerSeq &&  inner_seq,
OuterKeySelector &&  outer_key_sel,
InnerKeySelector &&  inner_key_sel,
ResultSelector &&  result_sel,
Pred &&  pred 
) -> detail::group_join_impl<InnerSeq, OuterKeySelector, InnerKeySelector, ResultSelector, Pred>

Extracts keys for the elements of an outer sequence (the one on which this operator is applied, e.g. the one passed to coveo::linq::from()) and the provided inner sequence using the provided key selectors. Next, creates groups of elements from the inner sequence with keys matching that of the elements in the outer sequence. Finally, the provided result selector is used to convert the groups into the final results. The result selector is called with two arguments: an element from the outer sequence and a group of elements from the inner sequence that share the same key.

In order to match the keys, they are compared using the provided predicate. The predicate must provide a strict ordering of the keys, like std::less.

Use like this:

using person_data = std::pair<int, std::string>;
using person_messages = std::pair<int, std::string>;
const std::vector<person_data> PERSONS = {
{ 1, "John Doe" },
{ 2, "Jane Smith" },
};
const std::vector<person_messages> MESSAGES = {
{ 1, "This is a test message" },
{ 2, "Hello Jane!" },
{ 1, "Doctors hate him!" },
{ 1, "Welcome to the company" },
{ 2, "Todo before we leave for vacation" },
};
using namespace coveo::linq;
auto seq = from(PERSONS)
| group_join(MESSAGES,
[](const person_data& pd) { return pd.first; },
[](const person_messages& pm) { return pm.first; }
[](const person_data& pd, const coveo::enumerable<const person_messages>& e) {
std::string res = pd.second + ": ";
for (auto&& pm : e) {
res += pm.second + ", ";
}
return res;
},
[](int i, int j) { return i > j; });
// seq == {
// "John Doe: This is a test message, Doctors hate him!, Welcome to the company, ",
// "Jane Smith: Hello Jane!, Todo before we leave for vacation, "
// }
Parameters
inner_seqInner sequence to scan to create groups.
outer_key_selSelector to get keys for elements in the outer sequence.
inner_key_selSelector to get keys for elements in the inner sequence.
result_selResult selector used to produce final results.
predPredicate used to compare keys.
Returns
(Once applied) Sequence of values returned by result_sel.