I started playing with Boost::Range in order to have a pipeline of lazy transforms in C++. My problem now is how to split a pipeline in smaller parts. Suppose I have:
int main(){
auto map = boost::adaptors::transformed; // shorten the name
auto sink = generate(1) | map([](int x){ return 2*x; })
| map([](int x){ return x+1; })
| map([](int x){ return 3*x; });
for(auto i : sink)
std::cout << i << "\n";
}
And I want to replace the first two maps with a magic_transform
, i.e.:
int main(){
auto map = boost::adaptors::transformed; // shorten the name
auto sink = generate(1) | magic_transform()
| map([](int x){ return 3*x; });
for(auto i : sink)
std::cout << i << "\n";
}
How would one write magic_transform
? I looked up Boost::Range's documentation, but I can't get a good grasp of it.
Addendum: I'm looking to write a class like this:
class magic_transform {
... run_pipeline(... input) {
return input | map([](int x){ return 2*x; })
| map([](int x){ return x+1; });
};
generate
? I am worried that yoursink
contains a reference to a temporary that expires at the semicolon. – Mcloughlingenerate
's return type isboost::iterator_range
, please check liveworkspace.org/code/841508d3b54bed4181d4e9fb6058200f for details. – Tallyman