Thu, 11. Mar 2010
Chris Sells translated a Clojure implementation of a function that descends into a directory in a "lazy" manner by Craig Andera into C# but his solution is so .NET 2.0 ;-).
Here's my take
static IEnumerable<string> GetDirectoryDescendants(string path) {
return Directory.GetFiles(path)
.Concat(Directory.GetDirectories(path)
.SelectMany<string, string>(GetDirectoryDescendants)
);
}
where I'd hope C# 4.0's compiler will be able to deduce the types
for the SelectMany itself. Interestingly the C# 3.0
compiler does fine when I use a lambda expression instead:
static IEnumerable<string> GetDirectoryDescendants(string path) {
return Directory.GetFiles(path)
.Concat(Directory.GetDirectories(path)
.SelectMany(dir => GetDirectoryDescendants(dir))
);
}
path: /en/dotNet | # | Writebacks