digitalmars.D - I like D
- Steven Schveighoffer (21/21) Mar 24 2015 Recently, while updating a D utility I wrote to process a binary log
- Dicebot (3/27) Mar 24 2015 Can be nice entry for weekly D newsletter tips & tricks.
- Adam D. Ruppe (2/3) Mar 24 2015 Aye, write it up!
- Walter Bright (2/5) Mar 24 2015 Also, as a Phobos example for take or cycle.
- "Jacques =?UTF-8?B?TcO8bGxlciI=?= <jacques.mueller gmx.de> (3/7) Mar 24 2015 output.writeln("\t-".repeat(nfields).join);
- Andrei Alexandrescu (4/11) Mar 24 2015 output.writeln("\t-".repeat(nfields).joiner);
- Steven Schveighoffer (7/18) Mar 24 2015 Yeah, I looked at repeat first, but it didn't do what I wanted (would
- =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= (4/24) Mar 25 2015 Also, the shortest solution:
- Dylan Knutson (5/12) Mar 24 2015 I see that `take` returns a lazy range. That's really neat! How
- Steven Schveighoffer (5/18) Mar 24 2015 writeln uses format, which takes a dchar output range to send the result...
- John Colvin (4/29) Mar 24 2015 I was gonna say that should be `takeExactly`, but I see that
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
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
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
On 3/24/2015 11:35 AM, Adam D. Ruppe wrote:On Tuesday, 24 March 2015 at 18:16:08 UTC, Dicebot wrote:Also, as a Phobos example for take or cycle.Can be nice entry for weekly D newsletter tips & tricks.Aye, write it up!
Mar 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);
Mar 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:output.writeln("\t-".repeat(nfields).joiner); AndreiNew code: immutable nfields = header.count('\t'); ... output.writeln(cycle("\t-").take(2 * nfields));output.writeln("\t-".repeat(nfields).join);
Mar 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: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! -SteveOn Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer wrote:output.writeln("\t-".repeat(nfields).joiner);New code: immutable nfields = header.count('\t'); ... output.writeln(cycle("\t-").take(2 * nfields));output.writeln("\t-".repeat(nfields).join);
Mar 24 2015
Am 24.03.2015 um 21:19 schrieb Steven Schveighoffer:On 3/24/15 4:11 PM, Andrei Alexandrescu wrote:Also, the shortest solution: import std.array; output.writeln("\t-".replicate(nfields));On 3/24/15 11:56 AM, "Jacques =?UTF-8?B?TcO8bGxlciI=?= <jacques.mueller gmx.de>" wrote: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! -SteveOn Tuesday, 24 March 2015 at 13:09:21 UTC, Steven Schveighoffer wrote:output.writeln("\t-".repeat(nfields).joiner);New code: immutable nfields = header.count('\t'); ... output.writeln(cycle("\t-").take(2 * nfields));output.writeln("\t-".repeat(nfields).join);
Mar 25 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. -SteveI 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
On 3/24/15 4:38 PM, Dylan Knutson wrote: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. -Steveimmutable 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. -SteveI 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
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. -SteveI was gonna say that should be `takeExactly`, but I see that `take` is specialised appropriately for infinite ranges. Hooray :)
Mar 24 2015