Can't rending DT template
I have this code: module pages; import vibe.d; import database; import vibe.web.web; class MyPages { @path("/page1") void getPage() { render!("home.dt"); } } The error: https://snag.gy/PtNeSs.jpg Error: template instance vibe.web.web.render!"home.dt".render!("pages", "pages.MyPages.getPage").render.compileHTMLDietFile!("home.dt", req, DefaultFilters).exec!(StreamOutputRange!(OutputStream)) error instantiating What I am doing wrong? Can I output html page instead of .dt?
Re: Can't rending DT template
It's look like wrong error, because project is buildable. So I have only one question. Can I do response not in .dt, but in .html?
Re: Can't rending DT template
On Sunday, 26 February 2017 at 12:31:21 UTC, Suliman wrote: It's look like wrong error, because project is buildable. So I have only one question. Can I do response not in .dt, but in .html? It's seems that serveStaticFiles is only one normal way to get it work.
Re: ddoc: Can I escape a colon?
On Thursday, 23 February 2017 at 21:04:39 UTC, Nick Sabalausky (Abscissa) wrote: Suppose I want ddoc output to include this line: -- Note: Blah blabbety blah -- But the colon causes "Note" to be considered a section header. Is there a way to escape the ":" so that it's displayed as expected, but doesn't trigger a section? You can put a space before : and ddoc, or ddox won't treat it as a section. - Note : Blah blabbety blah -
code D'ish enough?
Hi, coming more or less from Python I just started with D. Not a real programmer, just automating things and looking for a neat compiled language. Just to learn, I wrote a function to read CSV-like files (I know D has its own routine). Since I'm still a bit overwhelmed by the many complex language features, I'm curious what could I change to make my code as D'ish as possible? Thank you for any suggestion, Thorstein // Reads CSV-like files with only numeric values in each column // new_ndv replaces ndv, which is the original no-data-value double[][]* readNumMatCsv(char[] filePath, char[] ndv, char[] new_ndv) { double[][]* p_numArray; double[][] numArray; char[] line; string noCont = "File content not usable. Quit here."; string noFile = "Could not read file. Quit here."; string nonNum = "Found a non-numeric value in data matrix. Quit here."; Regex!char re = regex(r"(\n$)"); if(exists(filePath)) { File f = File(filePath, "r"); if((line = f.readln().dup).length > 0) { while (!f.eof()) { if((line = replaceAll(f.readln().dup.replace(ndv, new_ndv), re, "")).length > 0) { foreach(i;split(line,",")) { if(isNumeric(i) == false) { writeln(nonNum); return p_numArray; } } if(split(line,",").length > 0) { numArray ~= to!(double[])(split(line,",")); } } } p_numArray = &numArray; } else { writeln(noCont); } } else { writeln(noFile); } return p_numArray; }
code D'ish enough? - ignore previous post with same subject
Hi, sorry for posting again, but I used a keyboard combination that accidently send my post before it was done. Coming more or less from Python I just started with D. Not a real programmer, just automating things and looking for a neat compiled language. Just to learn, I wrote a function to read CSV-like files (I know D has its own routine). Since I'm still a bit overwhelmed by the many complex language features, I'm curious what could I change to make my code as D'ish as possible? Thank you for any suggestion, Thorstein // Reads CSV-like files with only numeric values in each column // new_ndv replaces ndv, which is the original no-data-value double[][]* readNumMatCsv(char[] filePath, int numHeaderLines, char[] ndv, char[] new_ndv) { double[][]* p_numArray; double[][] numArray; char[] line; string noCont = "File content not usable. Quit here."; string noFile = "Could not read file. Quit here."; string nonNum = "Found a non-numeric value in data matrix. Quit here."; Regex!char re = regex(r"(\n$)"); if(exists(filePath)) { File f = File(filePath, "r"); if((line = f.readln().dup).length > 0) { while (!f.eof()) // 1st replace ndv with new_ndv, 2nd remove all \n, 3rd check id size of read line >0 { if((line = replaceAll(f.readln().dup.replace(ndv, new_ndv), re, "")).length > 0) // check if all elements of splitted line are numeric { foreach(i;split(line,",")) { if(isNumeric(i) == false) // otherwise return pointer to empty array { writeln(nonNum); return p_numArray; } } // convert characters to double if(split(line,",").length > 0) { numArray ~= to!(double[])(split(line,",")); } } } // pass reference to pointer p_numArray = &numArray; // first line empty -> return pointer to empty array } else { writeln(noCont); } // file could not be find } else { writeln(noFile); } return p_numArray; }
Re: Can't rending DT template
On Sunday, 26 February 2017 at 12:22:01 UTC, Suliman wrote: I have this code: module pages; import vibe.d; import database; import vibe.web.web; class MyPages { @path("/page1") void getPage() { render!("home.dt"); } } The error: https://snag.gy/PtNeSs.jpg Error: template instance vibe.web.web.render!"home.dt".render!("pages", "pages.MyPages.getPage").render.compileHTMLDietFile!("home.dt", req, DefaultFilters).exec!(StreamOutputRange!(OutputStream)) error instantiating What I am doing wrong? Can I output html page instead of .dt? The error is some syntax or variable error in the diet file. Send us the whole error output and some part from the diet file if you want better feedback
Re: code D'ish enough? - ignore previous post with same subject
On Sunday, 26 February 2017 at 19:34:33 UTC, thorstein wrote: // Reads CSV-like files with only numeric values in each column // new_ndv replaces ndv, which is the original no-data-value double[][]* readNumMatCsv(char[] filePath, int numHeaderLines, char[] ndv, char[] new_ndv) * "no-data-value"? * Returning a pointer seems dubious. I'd expect double[][] to work. * It's not obvious to me what "Mat" means in "readNumMatCsv". * The char[] arguments should be const if possible. * You can probably add some attributes to the function: pure, @safe. { double[][]* p_numArray; This brace style is rather uncommon. double[][] numArray; char[] line; string noCont = "File content not usable. Quit here."; string noFile = "Could not read file. Quit here."; string nonNum = "Found a non-numeric value in data matrix. Quit here."; Those message constants should be enums or static immutable. Personally, I'd just put the literals directly in the writeln calls, as you're not using them only once. Regex!char re = regex(r"(\n$)"); if(exists(filePath)) { File f = File(filePath, "r"); if((line = f.readln().dup).length > 0) As far as I see, you're never using the line you're reading here. Does this just skip the header line? You're also not using numHeaderLines. Maybe this should loop numHeaderLines times? There's no need to assign to `line` then. Also no need to dup. { while (!f.eof()) // 1st replace ndv with new_ndv, 2nd remove all \n, 3rd check id size of read line >0 { if((line = replaceAll(f.readln().dup.replace(ndv, new_ndv), re, "")).length > 0) byLineCopy may be simpler to use here than readln. For this use case, byLine (no copy) could probably work, too. But byLineCopy is less bug prone, so better start with that one. https://dlang.org/phobos/std_stdio.html#.File.byLineCopy It's still not clear to me what's the deal with ndv and new_ndv. A line should contain at most one newline, at the end. There's a special function in std.string to remove an optional suffic: chomp. And with byLineCopy there's a parameter to omit the newline. I'd prefer both of those over regex here. https://dlang.org/phobos/std_string.html#.chomp // check if all elements of splitted line are numeric { foreach(i;split(line,",")) A string that's called "i" may be surprising to some. Could probably use the range version of `split`: std.algorithm.iteration.splitter. That would avoid an allocation. But split is ok if you're more comfortable with arrays than with ranges. https://dlang.org/phobos/std_algorithm_iteration.html#.splitter { if(isNumeric(i) == false) if (!isNumeric(i)) // otherwise return pointer to empty array { writeln(nonNum); return p_numArray; } } // convert characters to double if(split(line,",").length > 0) { numArray ~= to!(double[])(split(line,",")); You're executing `split(line, ",")` three times. Better do it just once and save the result in a (const) variable. Instead of checking beforehand if all values are numeric, you can also just let to!(double[]) fail, and catch the exception if you want. If I read it right, the `.length > 0` check means that blank lines are allowed at this point, right? Seems inconsistent to forbid a blank leading line but allow them later on. } } } // pass reference to pointer p_numArray = &numArray; No, no, no. This is a pointer to a local variable and later you return it. That's invalid, undefined behavior. Don't do it. Just return numArray itself. As expected, you don't need to return a pointer, just double[][] is fine. // first line empty -> return pointer to empty array } else { writeln(noCont); } // file could not be find } else { writeln(noFile); } Instead of the nested `if`s style, you could reverse the conditions and return early, or maybe throw an Exception: if (/* ... no file ... */) { writeln(noFile); return []; } if (/* ... no content ... */) { writeln(noCont); return []; } /* ... rest of the code ... */ That saves some indentation levels, and makes the purpose of those checks more obvious. return p_numArray; }
Re: code D'ish enough? - ignore previous post with same subject
On Sunday, 26 February 2017 at 19:34:33 UTC, thorstein wrote: Hi, sorry for posting again, but I used a keyboard combination that accidently send my post before it was done. Coming more or less from Python I just started with D. Not a real programmer, just automating things and looking for a neat compiled language. Just to learn, I wrote a function to read CSV-like files (I know D has its own routine). Since I'm still a bit overwhelmed by the many complex language features, I'm curious what could I change to make my code as D'ish as possible? Thank you for any suggestion, Thorstein // Reads CSV-like files with only numeric values in each column // new_ndv replaces ndv, which is the original no-data-value double[][]* readNumMatCsv(char[] filePath, int numHeaderLines, char[] ndv, char[] new_ndv) { double[][]* p_numArray; double[][] numArray; char[] line; string noCont = "File content not usable. Quit here."; string noFile = "Could not read file. Quit here."; string nonNum = "Found a non-numeric value in data matrix. Quit here."; Regex!char re = regex(r"(\n$)"); if(exists(filePath)) { File f = File(filePath, "r"); if((line = f.readln().dup).length > 0) { while (!f.eof()) // 1st replace ndv with new_ndv, 2nd remove all \n, 3rd check id size of read line >0 { if((line = replaceAll(f.readln().dup.replace(ndv, new_ndv), re, "")).length > 0) // check if all elements of splitted line are numeric { foreach(i;split(line,",")) { if(isNumeric(i) == false) // otherwise return pointer to empty array { writeln(nonNum); return p_numArray; } } // convert characters to double if(split(line,",").length > 0) { numArray ~= to!(double[])(split(line,",")); } } } // pass reference to pointer p_numArray = &numArray; // first line empty -> return pointer to empty array } else { writeln(noCont); } // file could not be find } else { writeln(noFile); } return p_numArray; } I'm in a similar boat, as I continue to learn D, I find myself using UFCS, "auto", and operating on ranges alot more; I think that's considered idiomatic. For example, here is my rough attempt: auto readNumMatCsv2 (string filePath, string ndv, string new_ndv){ double[][] p_numArray; try { auto lines = File(filePath,"r").byLine; lines.popFront; // get read of header p_numArray = lines.map!(a => a.replace (ndv,new_ndv) .splitter (",") .map!(a => a.to!double) .array) .array; } catch (Exception e){ e.msg.writeln; // this replaces "Could not read file. Quit here." } return p_numArray; } It took me quite a while to get the whole usage of range stuff like "map" and "filter" etc., but I think it's worth the effort.
Parallel foreach over AliasSec?
Hi, Is it possible to parallelise the iteration over an AliasSec? Ordinary parallel foreach does not work. I have tried submitting tasks to taskPool in an ordinary foreach, but I can't because i cannot be read at compile time. int one(int) {return 1;} int two(int) {return 2;} int three(int) {return 3;} int four(int) {return 4;} int five(int) {return 5;} int six(int) {return 6;} int seven(int) {return 7;} int eight(int) {return 8;} int[8] values; template eval_all(funcs...) { void eval_all(int val) { import std.parallelism; //foreach (i, f; parallel(funcs)) // Tries to evaluate f(void) foreach (i, f; funcs) // How do I parallelise this? values[i] = f(val); } } void main() { eval_all!(one, two, three, four, five, six, seven, eight)(42); foreach(i, val; values) assert(val == i + 1); } Thanks!
Re: Parallel foreach over AliasSec?
On 02/27/2017 01:35 AM, Bastiaan Veelo wrote: template eval_all(funcs...) { void eval_all(int val) { import std.parallelism; //foreach (i, f; parallel(funcs))// Tries to evaluate f(void) foreach (i, f; funcs)// How do I parallelise this? values[i] = f(val); } } Make a range or an array of function pointers from the AliasSeq of function aliases: import std.meta: staticMap; import std.range: only; enum fptr(alias f) = &f; enum fptrs = staticMap!(fptr, funcs); auto r = only(fptrs); foreach (i, f; parallel(r)) values[i] = f(val);
Re: trick to make throwing method @nogc
On Saturday, 25 February 2017 at 19:59:29 UTC, ikod wrote: Hello, I have a method for range: struct Range { immutable(ubyte[]) _buffer; size_t _pos; @property void popFront() pure @safe { enforce(_pos < _buffer.length, "popFront from empty buffer"); _pos++; } } I'd like to have @nogc here, but I can't because enforce() is non-@nogc. I have a trick but not sure if it is valid, especially I don't know if optimization will preserve code, used for throwing: import std.string; struct Range { immutable(ubyte[]) _buffer; size_t _pos; this(immutable(ubyte[]) s) { _buffer = s; } @property void popFront() pure @safe @nogc { if (_pos >= _buffer.length ) { auto _ = _buffer[$]; // throws RangeError } _pos++; } } void main() { auto r = Range("1".representation); r.popFront(); r.popFront(); // throws } Is it ok to use it? Is there any better solution? Thanks! solution 1/ === You can throw a static Exception. auto staticEx(string msg, string file = __FILE__, size_t line = __LINE__)() @nogc { immutable static Exception e = new Exception(msg, file, line); return e; } void main() @nogc { throw staticEx!"bla"; } not good for the call stack display, tho. solution 2/ === Throw an Error. Errors shouldn't be caugth and consequently they lead to program termination so you don't care about the leak. import std.experimental.allocator: make; import std.experimental.allocator.mallocator: Mallocator; void main() @nogc { throw make!Error(Mallocator.instance, "bla"); } good when errors are not designed to be caught. not good for exceptions because might leak to death depending on how the exceptions happens (i.e in a loop ouch). solution 3/ === Reference counting. Not explored so far. To finish, using a assert(0) is bad. assert(0) throws an error, it's really not like an Exception.
How to specify --arch option in dub.json
I use dub 1.2.1 to build my project. As convenience, I choose `dflags-windows-x86: ["-m32mscoff"]` option in dub.json of my library binding to specify default architecture in win32 platform. But in the newest dub release, this cause a warning which told me to use DLFAGS environment or specify arch in command line. Is there any other ways to fix this for I don't want users of this library always build with a long command line args?
Re: Parallel foreach over AliasSec?
On Monday, 27 February 2017 at 02:02:57 UTC, ag0aep6g wrote: Make a range or an array of function pointers from the AliasSeq of function aliases: import std.meta: staticMap; import std.range: only; enum fptr(alias f) = &f; enum fptrs = staticMap!(fptr, funcs); auto r = only(fptrs); foreach (i, f; parallel(r)) values[i] = f(val); Wow. Thank you!