Monday, May 07, 2007

Nesting Splitters

To recap on my previous post - C# provides the "yield" keyword that allows any function that returns an IEnumerable object to provide lazy iteration capability (a Custom Enumerator). This allows us to iterate across an array or list and filter elements, convert them to different types, aggregate them with another list etc.

My last post detailed how to "Split" an IEnumerable into two separate mutually exclusive enumerators. I.e. the equivalent of an if-else statement acting on each element.

At the end of the post I suggested that it would be great to output multiple enumerators i.e. the equivalent of if-then-else or switch type patterns. This may not be required as we can Nest Enumerators!

It goes like this:

inputList => split1 and split2 using our first test/predicate
split1 => split3 and split4 using our second test/predicate on the results of split1

We can then split the results add infinitum - creating a Binary Tree Splitter. At each node in the tree we queue the results of the "lagging" enumerator until it catches up. What is the benefit?

We can split out multiple enumerators and process each of them differently.

0 Comments:

Post a Comment

<< Home