www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - For fun: Expressive C++ 17 Coding Challenge in D

reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
Found on Reddit:

 
https://www.reddit.com/r/programming/comments/740617/the_expressive_c17_coding_challenge/

How would you do it in D?

Ali

P.S. You can ignore the following note from the challenge text; I don't 
think it applies to D. Honestly, I don't think it matters for C++17 
either. :)

   "You can assume input files won't be super large and can fit fully 
into memory."
Oct 03 2017
next sibling parent reply Biotronic <simen.kjaras gmail.com> writes:
On Tuesday, 3 October 2017 at 19:25:56 UTC, Ali Çehreli wrote:
 Found on Reddit:


 https://www.reddit.com/r/programming/comments/740617/the_expressive_c17_coding_challenge/

 How would you do it in D?

 Ali

 P.S. You can ignore the following note from the challenge text; 
 I don't think it applies to D. Honestly, I don't think it 
 matters for C++17 either. :)

   "You can assume input files won't be super large and can fit 
 fully into memory."
https://gist.github.com/Biotronic/0bc6048b880d67bfdca970453cc47cf9 I opted for writing to stdout instead, because 1) it's easier, x) it's less code, and b) it's more flexible. The memory limitations certainly do apply - readText would fail upon reading humongous files, and for 32-bit programs the resulting string wouldn't be able to hold enough data. Since the code uses ranges though, a simple replacement of readText with an mmapped equivalent should enable humongous file support with no other code change required. -- Biotronic
Oct 04 2017
next sibling parent Biotronic <simen.kjaras gmail.com> writes:
On Wednesday, 4 October 2017 at 09:04:58 UTC, Biotronic wrote:
 Since the code uses ranges though, a simple replacement of 
 readText with an mmapped equivalent should enable humongous 
 file support with no other code change required.
Drop-in replacement for readText: struct MmText { import std.mmfile; ulong _fileOffset; MmFile _file; this(string filename) { _file = new MmFile(filename); } dchar front() { auto end = min(_file.length, _fileOffset+4); auto data = cast(string)_file[_fileOffset..end]; return decodeFront(data); } void popFront() { auto end = min(_file.length, _fileOffset+4); auto data = cast(string)_file[_fileOffset..end]; size_t bytes; decodeFront(data, bytes); _fileOffset += bytes; } bool empty() { return _fileOffset >= _file.length; } } -- Biotronic
Oct 04 2017
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Oct 04 2017
next sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Wednesday, 4 October 2017 at 15:26:02 UTC, Ali Çehreli wrote:
 On 10/04/2017 02:04 AM, Biotronic wrote:
... Hey where is the list of features used e.g: ranges, ufcs...
Oct 04 2017
parent reply Biotronic <simen.kjaras gmail.com> writes:
On Wednesday, 4 October 2017 at 19:20:12 UTC, Jesse Phillips 
wrote:
 On Wednesday, 4 October 2017 at 15:26:02 UTC, Ali Çehreli wrote:
 On 10/04/2017 02:04 AM, Biotronic wrote:
... Hey where is the list of features used e.g: ranges, ufcs...
Features used: D. But sure, added them to the gist: https://gist.github.com/Biotronic/0bc6048b880d67bfdca970453cc47cf9 Also added some more stuff to show off more D features, like unittests and safe. -- Biotronic
Oct 05 2017
parent kerdemdemir <kerdemdemir hotmail.com> writes:
I am a total beginner but I want to post that a lot.

auto autoCorrelation(R)(R range)
         if (isRandomAccessRange!R)
{

	import std.numeric : fft, inverseFft;
	import std.range : chain, repeat, zip, dropBack;
	import std.algorithm : map;
	import std.complex;
	
	auto residual = residualPowerOf2(range.length);
	auto fftResult = range.chain(repeat(0, residual)).fft();
	auto autoCorrResult = fftResult.zip(fftResult.map!(a => 
a.conj())).
							map!( a=> a[0] * a[1] ).
							inverseFft().
							dropBack(residual).
							map!( a => a.re );
				
	return autoCorrResult;
}	

I implemented auto correlation in C++ before which is I believe 
2~3 time bigger(also I needed to compile fftw, lapack etc.. ) :
https://forum.kde.org/viewtopic.php?f=74&t=118619 .

That was the moment I feel in love with D.
Oct 06 2017
prev sibling parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
Another solution using dlangs builtin csv support for reading.

import std.csv;
import std.file;
import std.algorithm : map;
import std.range;

string csvWrite(Header, Rows)(Header header, Rows rows)
{
    return header.join(",") ~ "\n" ~ rows.map!(r => header.map!(h =>
r[h]).join(",")).join("\n");
}

int main(string[] args)
{
    auto inputFile = args[1];
    auto columnName = args[2];
    auto replacement = args[3];
    auto outputFile = args[4];

    auto records = readText(inputFile).csvReader!(string[string])(null);
    write(outputFile, csvWrite(records.header, records.map!((r) {
                r[columnName] = replacement;
                return r;
            })));
    return 0;
}

Unfortunately this is still far from the powershell solution :/

cK
Oct 15 2017
prev sibling parent reply Atila Neves <atila.neves gmail.com> writes:
On Tuesday, 3 October 2017 at 19:25:56 UTC, Ali Çehreli wrote:
 Found on Reddit:


 https://www.reddit.com/r/programming/comments/740617/the_expressive_c17_coding_challenge/

 How would you do it in D?

 Ali

 P.S. You can ignore the following note from the challenge text; 
 I don't think it applies to D. Honestly, I don't think it 
 matters for C++17 either. :)

   "You can assume input files won't be super large and can fit 
 fully into memory."
I can't bring myself to code a solution in C++17 or D. In C++17 it'd be too painful, and in D so trivial it'd probably make me sleep out of boredom. Maybe that should be our new catchphrase: "D: Making programming boring" :P Atila
Oct 04 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/04/2017 02:26 AM, Atila Neves wrote:

 in D so trivial it'd probably make me sleep out of boredom.
I spent more time on this obviously trivial program than necessary. :( In addition to facing known template resolution issues, the hidden \r characters at the ends of some of the fields in the example textcolumns threw me off for a while. (Compounded by silly mistakes that I was making.)
 Maybe that should be our new catchphrase:

 "D: Making programming boring"
While still keeping it interesting enough to scratch your head once in a while. :) Ali
Oct 04 2017
next sibling parent jmh530 <john.michael.hall gmail.com> writes:
On Wednesday, 4 October 2017 at 15:30:08 UTC, Ali Çehreli wrote:
 On 10/04/2017 02:26 AM, Atila Neves wrote:

 in D so trivial it'd probably make me sleep out of boredom.
I spent more time on this obviously trivial program than necessary. :( In addition to facing known template resolution issues, the hidden \r characters at the ends of some of the fields in the example textcolumns threw me off for a while. (Compounded by silly mistakes that I was making.)
But it's not wasted time. You could also use it somewhere else. Blog post, updated version of your book, new book, etc.
Oct 04 2017
prev sibling parent lithium iodate <whatdoiknow doesntexist.net> writes:
On Wednesday, 4 October 2017 at 15:30:08 UTC, Ali Çehreli wrote:
 the hidden \r characters at the ends
Those got me too! Here's my less than optimal solution: int main(string[] args) { import std.stdio; import std.algorithm.iteration : map, splitter, joiner, each; import std.algorithm.searching : countUntil; import std.range : enumerate; import std.string : chomp; if (args.length != 5 && args.length != 4) { stderr.writeln("Something went wrong and it's obviously your fault."); return 1; } immutable columnID = args[2]; immutable substitute = args[3]; auto data = File(args[1], "r").byLine.map!chomp; if (data.empty) { stderr.writeln("input file missing\n\n(actually it exists, it's just " ~ "empty)\n\n(your fault regardless)"); return 1; } File output; if (args.length == 5) output = File(args[4], "w"); else output = stdout; immutable matchedColumn = data.front.splitter(",").countUntil(columnID); if (matchedColumn < 0) { stderr.writeln("column name doesn’t exist in the input file\n\n(and " ~ "it's your fault)"); return 1; } output.writeln(data.front); data.popFront; data.map!(line => line .splitter(",") .enumerate .map!(a => a.index == matchedColumn ? substitute : a.value) .joiner(",")).each!(a => output.writeln(a)); return 0; } I think the biggest problem is the lack of support for quoted content.
Oct 04 2017