Consider the following extension method in c#, Traverse:
IEnumerable<T> Traverse<T>( this IEnumerable<T> source,
Func<T, IEnumerable<T>> fnRecurse );
This method allows one to recurse through a tree as defined by T and whatever function causes T to return its subnodes.
Now consider the following implementation of T:
class Node
{
public string Name;
public List<Node> Children;
}
My goal is to write the shortest function possible that will return an IEnumerable containing the fully qualified paths for every node in this tree. Something like:
var node = GetParentNode();
return node.Traverse( node => node.Children )
.Select( node => GetParentName(node) + ":" + node.Name );
Obviously, adding a Parent property to Node makes the problem trivial. Instead I'd like to build my parent strings inside a functor somehow. I don't think this would be too hard in C++ but I don't see how to do it in C#. Any ideas?