I’m having a hard time finding the right LINQ syntax to use for the following iterator block:
class Program
{
class Operation
{
public IEnumerable<Operation> NextOperations { get; private set; }
}
class Item { }
static Item GetItem(Operation operation)
{
return new Item();
}
static IEnumerable<Item> GetItems(IEnumerable<Operation> operations)
{
foreach (var operation in operations)
{
yield return GetItem(operation);
foreach (var item in GetItems(operation.NextOperations)) // recursive
yield return item;
}
}
static void Main(string[] args)
{
var operations = new List<Operation>();
foreach (var item in GetItems(operations))
{
}
}
}
Maybe what I have is as good as it gets? For this particular code, yield return inside an explicit foreach is indeed the right solution?
It’s pretty good. We can make it slightly better.
It’s a reasonable solution. It’s easy to read and clearly correct. The down side is, as I mentioned earlier, that the performance is potentially not good if the tree is extremely deep.
Here’s how I would do this:
Note that the call to Reverse() is necessary only if you care that the iteration go “in order”. For example, suppose operation Alpha has child operations Beta, Gamma and Delta, and Delta has children Zeta and Omega. The traversal goes like this:
and now the stack is empty so we’re done, and we get the items in “preorder traversal” order. If you don’t care about the order, if all you need is to make sure you get all of them, then don’t bother to Reverse the children, and you’ll get them in the order Alpha, Delta, Omega, Zeta, Gamma, Beta.
Make sense?