title: C++ Iterator Pipelining Designs
body: <p>Suppose we want to apply a series of transformations, <code>int f1(int)</code>, <code>int f2(int)</code>, <code>int f3(int)</code>, to a list of objects.  A naive way would be</p>
  
  <pre><code>SourceContainer source;
  
  TempContainer1 temp1;
  transform(source.begin(), source.end(), back_inserter(temp1), f1);
  TempContainer2 temp2;
  transform(temp1.begin(), temp1.end(), back_inserter(temp2), f2);
  
  TargetContainer target;
  transform(temp2.begin(), temp2.end(), back_inserter(target), f3);
  </code></pre>
  
  <p>This <strong>first</strong> solution is not optimal because of the extra space requirement with <code>temp1</code> and <code>temp2</code>.  So, let's get smarter with this:</p>
  
  <pre><code>int f123(int n) { return f3(f2(f1(n))); }
  ...
  SourceContainer source;
  TargetContainer target;
  transform(source.begin(), source.end(), back_inserter(target), f123);
  </code></pre>
  
  <p>This <strong>second</strong> solution is much better because not only the code is simpler but more importantly there is less space requirement without the intermediate calculations. </p>
  
  <p>However, the composition <code>f123</code> must be determined at compile time and thus is fixed at run time. </p>
  
  <p>How would I try to do this efficiently if the composition is to be determined at run time?  For example, if this code was in a RPC service and the actual composition--which can be any permutation of any subset of <code>f1</code>, <code>f2</code>, and <code>f3</code>--is based on arguments from the RPC call.</p>