Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 03, 2017 For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
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." |
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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
|
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Biotronic | 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 |
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Biotronic | 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 |
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Atila Neves | 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 |
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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...
|
October 04, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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. |
October 05, 2017 Re: For fun: Expressive C++ 17 Coding Challenge in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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 |
Copyright © 1999-2021 by the D Language Foundation