I am upgrading to C++ 11 and have basic idea about lambda expression as mentioned in https://learn.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019
I have following doubts about the code below captured from http://project-thrill.org/
The following program counts the number of occurrences of each unique word in a text
void WordCount(thrill::Context& ctx, std::string input, std::string output)
{
using Pair = std::pair<std::string, size_t>;
auto word_pairs = ReadLines(ctx, input)
.template FlatMap<Pair>(
// flatmap lambda: split and emit each word
[](const std::string& line, auto emit)
{
Split(line, ’ ’, [&](std::string_view sv)
{
emit(Pair(sv.to_string(), 1));
});
});
word_pairs.ReduceByKey(
// key extractor: the word string
[](const Pair& p) { return p.first; },
// commutative reduction: add counters
[](const Pair& a, const Pair& b)
{
return Pair(a.first, a.second + b.second);
})
.Map([](const Pair& p)
{
return p.first + ": " + std::to_string(p.second);
}).WriteLines(output);
}
first question what is
.template FlatMap
is FlatMap
a template type lambda function which is operating on return of ReadLines
?
inside
FlatMap<Pair>
how the value is geting passed to(const std::string& line, auto emit)
and who passes the value ?
inside
ReduceByKey
function, to the argument[](const Pair& p)
of lambda function, how value is getting passed ?