www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I like D

reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Mar 24 2015
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
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.
Can be nice entry for weekly D newsletter tips & tricks.
Mar 24 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 24 March 2015 at 18:16:08 UTC, Dicebot wrote:
 Can be nice entry for weekly D newsletter tips & tricks.
Aye, write it up!
Mar 24 2015
parent Walter Bright <newshound2 digitalmars.com> writes:
On 3/24/2015 11:35 AM, Adam D. Ruppe wrote:
 On Tuesday, 24 March 2015 at 18:16:08 UTC, Dicebot wrote:
 Can be nice entry for weekly D newsletter tips & tricks.
Aye, write it up!
Also, as a Phobos example for take or cycle.
Mar 24 2015
prev sibling next sibling parent reply "Jacques =?UTF-8?B?TcO8bGxlciI=?= <jacques.mueller gmx.de> writes:
On Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer 
wrote:
 New code:

 immutable nfields = header.count('\t');
 ...
 output.writeln(cycle("\t-").take(2 * nfields));
output.writeln("\t-".repeat(nfields).join);
Mar 24 2015
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/24/15 11:56 AM, "Jacques =?UTF-8?B?TcO8bGxlciI=?= 
<jacques.mueller gmx.de>" wrote:
 On Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer wrote:
 New code:

 immutable nfields = header.count('\t');
 ...
 output.writeln(cycle("\t-").take(2 * nfields));
output.writeln("\t-".repeat(nfields).join);
output.writeln("\t-".repeat(nfields).joiner); Andrei
Mar 24 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/24/15 4:11 PM, Andrei Alexandrescu wrote:
 On 3/24/15 11:56 AM, "Jacques =?UTF-8?B?TcO8bGxlciI=?=
 <jacques.mueller gmx.de>" wrote:
 On Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer wrote:
 New code:

 immutable nfields = header.count('\t');
 ...
 output.writeln(cycle("\t-").take(2 * nfields));
output.writeln("\t-".repeat(nfields).join);
output.writeln("\t-".repeat(nfields).joiner);
Yeah, I looked at repeat first, but it didn't do what I wanted (would only repeat a single element). For some reason, this seems more complex to me. However, it does have the benefit of not needing the '2 *'. But I did not think of using joiner, nice solution! -Steve
Mar 24 2015
parent =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= <sludwig rejectedsoftware.com> writes:
Am 24.03.2015 um 21:19 schrieb Steven Schveighoffer:
 On 3/24/15 4:11 PM, Andrei Alexandrescu wrote:
 On 3/24/15 11:56 AM, "Jacques =?UTF-8?B?TcO8bGxlciI=?=
 <jacques.mueller gmx.de>" wrote:
 On Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer wrote:
 New code:

 immutable nfields = header.count('\t');
 ...
 output.writeln(cycle("\t-").take(2 * nfields));
output.writeln("\t-".repeat(nfields).join);
output.writeln("\t-".repeat(nfields).joiner);
Yeah, I looked at repeat first, but it didn't do what I wanted (would only repeat a single element). For some reason, this seems more complex to me. However, it does have the benefit of not needing the '2 *'. But I did not think of using joiner, nice solution! -Steve
Also, the shortest solution: import std.array; output.writeln("\t-".replicate(nfields));
Mar 25 2015
prev sibling next sibling parent reply "Dylan Knutson" <tcdknutson gmail.com> writes:
 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 see that `take` returns a lazy range. That's really neat! How does writeln handle the lazy range though? Standard way of dealing with ranges in D, with calls to empty, front, and popFront (as in, does this code allocate any temporaries at all with the GC?)
Mar 24 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/24/15 4:38 PM, Dylan Knutson wrote:
 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 see that `take` returns a lazy range. That's really neat! How does writeln handle the lazy range though? Standard way of dealing with ranges in D, with calls to empty, front, and popFront (as in, does this code allocate any temporaries at all with the GC?)
writeln uses format, which takes a dchar output range to send the result to. This in turn uses fputc on the stream (shudder). So no GC allocations are needed. -Steve
Mar 24 2015
prev sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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 :)
Mar 24 2015