Jump to page: 1 2
Thread overview
I like D
Mar 24, 2015
Dicebot
Mar 24, 2015
Adam D. Ruppe
Mar 24, 2015
Walter Bright
Mar 24, 2015
Jacques Müller
Mar 25, 2015
Sönke Ludwig
Mar 24, 2015
Dylan Knutson
Mar 24, 2015
John Colvin
March 24, 2015
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
March 24, 2015
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.
March 24, 2015
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!
March 24, 2015
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);
March 24, 2015
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.
March 24, 2015
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
March 24, 2015
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
March 24, 2015
> 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?)
March 24, 2015
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 :)
March 24, 2015
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
« First   ‹ Prev
1 2