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

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

Functions

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

Detailed Description

The 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 joins elements from the inner sequence to elements in the outer sequence with matching keys. Finally, a result selector is used to fold the elements into the final results. (This operator is similar to a database JOIN.)

.NET equivalent: Join

Function Documentation

◆ join() [1/2]

template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector >
auto coveo::linq::join ( InnerSeq &&  inner_seq,
OuterKeySelector &&  outer_key_sel,
InnerKeySelector &&  inner_key_sel,
ResultSelector &&  result_sel 
) -> detail::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, joins elements from the inner sequence with elements in the outer sequence with matching keys. Finally, the provided result selector is used to convert the elements into the final results. The result selector is called with two arguments: an element from the outer sequence and an element 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)
| join(MESSAGES,
[](const person_data& pd) { return pd.first; },
[](const person_messages& pm) { return pm.first; }
[](const person_data& pd, const person_messages& pm) {
return pd.second + ": " + pm.second;
});
// seq == {
// "John Doe: This is a test message",
// "John Doe: Doctors hate him!",
// "John Doe: Welcome to the company",
// "Jane Smith: Hello Jane!",
// "Jane Smith: Todo before we leave for vacation"
// }
Parameters
inner_seqInner sequence to scan.
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.

◆ join() [2/2]

template<typename InnerSeq , typename OuterKeySelector , typename InnerKeySelector , typename ResultSelector , typename Pred >
auto coveo::linq::join ( InnerSeq &&  inner_seq,
OuterKeySelector &&  outer_key_sel,
InnerKeySelector &&  inner_key_sel,
ResultSelector &&  result_sel,
Pred &&  pred 
) -> detail::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, joins elements from the inner sequence with elements in the outer sequence with matching keys. Finally, the provided result selector is used to convert the elements into the final results. The result selector is called with two arguments: an element from the outer sequence and an element 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)
| join(MESSAGES,
[](const person_data& pd) { return pd.first; },
[](const person_messages& pm) { return pm.first; }
[](const person_data& pd, const person_messages& pm) {
return pd.second + ": " + pm.second;
},
[](int i, int j) { return i > j; });
// seq == {
// "John Doe: This is a test message",
// "John Doe: Doctors hate him!",
// "John Doe: Welcome to the company",
// "Jane Smith: Hello Jane!",
// "Jane Smith: Todo before we leave for vacation"
// }
Parameters
inner_seqInner sequence to scan.
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 the keys.
Returns
(Once applied) Sequence of values returned by result_sel.