title: How to compose iterators?
body: <p>I have a network of nodes passing structured data in between. For my subproblem, we have this branch - linear sequence of nodes:</p>
  
  <pre><code>nodes = [source, n1, n2, n3, n4]
  </code></pre>
  
  <p>First node is a generator, each other node gets values from input node and provides output value. Current implementation is plain get() from a pipe and put() to a pipe and there is separate thread per node (there is reason for that). I want to change it to a <code>yield</code>-ing iterator.</p>
  
  <p>And I want to evaluate in a following way (if we consider node to be a callable):</p>
  
  <pre><code>for result in n4(n3(n2(n1(source()))):
      print result
  </code></pre>
  
  <p>I imagine the construction of evaluation context like this:</p>
  
  <pre><code>context = src
  for node in nodes[1:]:
      context = pipe(context, node)
  
  for result in context:
      print result
  </code></pre>
  
  <p><strong>Limitations:</strong></p>
  
  <p>I still want to be able to use nodes separately - not nested, piping data by other means, because the nodes might be in separate threads. Example: <code>[source, n1,n2]</code> in one thread (might be nested), <code>[n3, n4]</code> in the other (might be nested), data piped between <code>n2</code> and <code>n3</code>. Case: there might be a non-linear node graph where I want to group branches this way.</p>
  
  <p><code>node</code> has to be a class to hold computation state</p>
  
  <p>How the implementation of the <code>context</code> and the <code>pipe(context, node)</code> might look like? Or if it can be solved in a different way, do you have any hints?</p>
  
  <p>Can <code>yield from</code> in Python 3.3 <a href="http://www.python.org/dev/peps/pep-0380/" rel="nofollow">(PEP380)</a> help my case in any way?</p>