Output Iterator Stages

If you need to transform a single input value into several output values, e.g. when you split up a file into lines, then this transformation can occupy its thread for quite some time. It thereby blocks other stages from running.

To improve the situation, such a transformation stage can pass on output values using yield return, e.g.

var flow = Flow<string>.DoWithIterator<string>(SplitFileIntoLines).Do<string>(ProcessLine);
...
IEnumerator<string> SplitFileIntoLines(string filename)
{
  using(StreamReader sr = ...)
  {
    while(!sr.EndOfStream)
      yield return sr.ReadLine();
   yield return null; // mark end of file
  }
}

The stage handler just needs to return IEnumerator<TOut> instead of TOut as the output type and use yield return instead of plain return. Also it needs to be fit into the flow using DoWithIterator<>() instead of Do<>().

The latter is due to the C# compiler having a hard time to distinguish Func<TIn, TOut> from Func<TIn, IEnumerator<TOut>>; overloading Do<>() with SeqIteratorHandler<> in addition to Func<> and SeqHandler<> did not work.

Last edited Jul 8, 2009 at 12:14 AM by ralfw, version 1

Comments

No comments yet.