Я обновляюсь до C ++ 11 и имею базовое представление о лямбда-выражении, как упомянуто в https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019
У меня следующие сомнения по поводу кода ниже, захваченного из http://project -thrill.org /
Следующая программа подсчитывает количество вхождений каждого уникального слова в тексте
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);
}
первый вопрос что такое .template FlatMap
- это FlatMap
лямбда-функция типа шаблона, которая работает по возвращении ReadLines
?
внутри FlatMap<Pair>
как значение передается (const std::string& line, auto emit)
и кто передает значение?
внутри ReduceByKey
функции, как аргумент [](const Pair& p)
лямбда-функции, как значение передается?