Coercing ranges to the same type
Matt Kline via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 6 16:14:26 PDT 2015
As it turns out, inputRangeObject does an excellent job at this
task. The solution then becomes something like:
InputRange!string getEntries(string[] paths, bool recursive)
{
auto files = paths.filter!(p => p.isFile);
if (recursive) {
auto expandedDirs = paths
.filter!(p => p.isDir)
.map!(p => dirEntries(p, SpanMode.depth, false))
.joiner
.map!(de => de.name);
return inputRangeObject(chain(files, expandedDirs));
}
else {
foreach (dir; paths.filter!(p => p.isDir))
stderr.writeln("omitting directory " , dir);
return inputRangeObject(files);
}
}
More information about the Digitalmars-d-learn
mailing list