Composing adaptors in Boost::range

Posted by bruno nery on Stack Overflow See other posts from Stack Overflow or by bruno nery
Published on 2012-11-05T22:57:30Z Indexed on 2012/11/05 22:59 UTC
Read the original article Hit count: 170

I started playing with Boost::Range in order to have a pipeline of lazy transforms in C++]1. 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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost