For fun: Expressive C++ 17 Coding Challenge in D
Ali Çehreli
acehreli at yahoo.com
Wed Oct 4 15:26:02 UTC 2017
On 10/04/2017 02:04 AM, Biotronic wrote:
> I opted for writing to stdout instead, because 1) it's easier, x) it's
> less code, and b) it's more flexible.
Exactly! :)
> a simple replacement of readText with an mmapped equivalent should
> enable humongous file support with no other code change required.
Here is one from me:
import std.stdio;
import std.algorithm;
import std.string;
import std.range;
const delim = ",";
auto byColumn(R)(R range) {
return range
.splitter(delim)
.map!strip;
}
int main(string[] args) {
if (args.length != 3) {
stderr.writefln("USAGE: %s <column-name> <replacement>", args[0]);
return 1;
}
const columnName = args[1];
auto lines = stdin.byLine;
const columnNumber = lines
.front
.byColumn
.countUntil(columnName);
if (columnNumber == -1) {
stderr.writefln(`ERROR: Failed to find "%s".`, columnName);
return 1;
}
writeln(lines.front);
lines.popFront();
const replacement = args[2];
auto replaced = lines
.map!(line => line
.byColumn
.enumerate
.map!(t => (t[0] == columnNumber) ?
replacement : t[1])
.joiner(delim));
writefln("%(%s\n%)", replaced);
return 0;
}
Ali
More information about the Digitalmars-d-learn
mailing list