On Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer
wrote:
Recently, while updating a D utility I wrote to process a
binary log file into a tab-separated text file, I found a nice
use for ranges.
So I have N columns, separated by tabs. The header looks like
this:
"TIME\tCOL1\tCOL2\t..."
With about 30 or 40 columns.
The first output is a line like this:
03/24/2015 12:34:56\t-\t-\t-\t...
to designate the start of the log file. In my code, I had, in
addition to printing the time, a line like this:
output.writeln("\t-\t-\t-\t-...");
So adding new columns, I now had to add an appropriate number
of "\t-" to this output. In this update, I also decided to
print one of these whenever the log "restarts", so now I would
have multiple lines like this to maintain. How annoying.
Luckily I was using D :)
New code:
immutable nfields = header.count('\t');
...
output.writeln(cycle("\t-").take(2 * nfields));
Super-win, now I only ever have to update the original column
list.
Little things like this are what make me love D.
-Steve
I was gonna say that should be `takeExactly`, but I see that
`take` is specialised appropriately for infinite ranges. Hooray :)