1

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 ?

Aman
  • 339
  • 3
  • 12
  • 2
    Your first question is answered here: https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords – NathanOliver May 09 '19 at 13:13
  • 1
    FlatMap is not a lambda, it takes a function as parameter and that parameter is a lambda that will be wrapped as a function. – Matthieu Brucher May 09 '19 at 13:18

1 Answers1

2

what is .template FlatMap

It's a syntactic necessity, to indicate that FlatMap is a template member, and that the < is part of an explicit template argument and not a comparison operator.

is FlatMap a template type lambda function which is operating on return of ReadLines?`

That's not a thing. FlatMap is a template member function, and it receives a Callable as it's sole parameter. Here it is called with a lambda expression.

inside FlatMap<Pair> how the value is geting passed to (const std::string& line, auto emit) and who passes the value ?

The body of FlatMap. like how emit is called inside the lambda. Presumably it is called once for each line in the input, and generates multiple Pair as output from each input line.

inside ReduceByKey function, to the argument [](const Pair& p) of lambda function, how value is getting passed ?

The same way. ReduceByKey has two Callable arguments, and calls them based on the member's of word_pairs (which is the result of FlatMap)

It will categorise each item it sees by the first function, and then combine within each category with the second function.

Caleth
  • 52,200
  • 2
  • 44
  • 75