functional_cpp
functional_cpp copied to clipboard
Improve performance with lazy operations
Consider the following example from the documentation
const auto employees_below_40 = ages
.zip(names)
.map<person>([](const auto& pair) {
return person(pair.first, pair.second);
})
.filter([](const auto& person) {
return person.age < 40;
})
.sort([](const auto& person1, const auto& person2) {
return person1.age < person2.age;
});
This snippet will iterate the corresponding instance of functional_vector
4 times, once for every call (zip
, map
, filter
and sort
). If ages
would have been a significantly long vector, then the current implementation is really inefficient.
In such cases it would be really useful if we had lazy variants of these algorithms, such that for the final result only one iteration would be performed. A possible but not definite implementation could look like this
const auto employees_below_40 = ages
.lazy_zip(names) // lazy on the top-level call, which propagates down the chain
.map<person>([](const auto& pair) {
return person(pair.first, pair.second);
})
.filter([](const auto& person) {
return person.age < 40;
})
.sort([](const auto& person1, const auto& person2) {
return person1.age < person2.age;
})
.get_result(); // or .execute();