The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Pascal Menuet (6) [Avatar] Offline
#1
The current implementation of append_name_if uses an undefined variable filter.
Maybe we could replace the example by something like that:
<typename FilterFunction>
auto append_name_if(FilterFunction filter) {
  return [filter](std::vector<std::string> previously_collected, const person_t &person) {
    if (filter(person))
      previously_collected.push_back(name(person));
    return previously_collected;
  };
}

// ...
template <typename FilterFunction, typename Iterator>
std::vector<std::string> names_for(
        Iterator people_begin,
        Iterator people_end,
        FilterFunction filter)
{
    return std::accumulate(
        people.cbegin(),
        people.cend(),
        std::vector<std::string>{},
        append_name_if(filter));
}
Ivan Cukic (104) [Avatar] Offline
#2
The assumption here is that filter is a free-standing function available in the scope. This snippet is meant to demonstrate the accumulation, I couldn't use lambdas yet, since they are introduced in the next chapter.

I'll see whether I can make this explicit to avoid confusion.

Thanks for the report! smilie
Pascal Menuet (6) [Avatar] Offline
#3
OK I see. Yes, it would be too soon in chronology to use a function that returns a lambda here.
But, yes, maybe, a declaration of a free-standing function filter could make the code clearer (at least for me smilie).