Search code examples
c#linqiteratoryield-return

iterator block to LINQ


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?


Solution

  • Maybe what I have is as good as it gets?

    It's pretty good. We can make it slightly better.

    For this particular code, yield return inside an explicit foreach is indeed the right solution?

    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:

    static IEnumerable<T> AllNodes(this T root, Func<T, IEnumerable<T>> getChildren) 
    {
        var stack = new Stack<T>();
        stack.Push(root);
        while(stack.Count > 0)
        {
            var current = stack.Pop();
            yield return current;
            foreach(var child in getChildren(current).Reverse())
                stack.Push(child);
        }
    } 
    
    static void Main()      
    {      
        var operation = whatever;
        var items = from op in operation.AllNodes(x=>x.NextOperations)
                    select GetItem(op);
        foreach (var item in items)      
        {      
        }      
    } 
    

    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:

    push Alpha
    pop Alpha
    yield Alpha
    push Delta
    push Gamma 
    push Beta
    pop Beta
    yield Beta
    pop Gamma
    yield Gamma
    pop Delta
    yield Delta
    push Omega
    push Zeta
    pop Zeta
    yield Zeta
    pop Omega
    yield Omega
    

    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?