Re: "Programming in D" is up-to-date
On Saturday, 13 May 2017 at 23:22:41 UTC, Ali Çehreli wrote: I've updated the book to 2.074.0. I've updated all paper and electronic versions at all publishers. However, I recommend that you wait a week or so before ordering (e.g. from Amazon) so that you get the latest version. (The copyright and Preface pages should say May 2017.) You can download the up-to-date versions here: http://ddili.org/ders/d.en/index.html The fonts are indeed embedded in the PDF, EPUB, and AZW3 formats. You may have to experiment with configuration settings of your e-reader to enable the embedded fonts. YMMV. :/ Ali I just wanted to say thank you very much for your book. It has been invaluable to me while learning D. Keep up the great work!
Re: Dlang Features You Would Like To Share
On Thursday, 13 April 2017 at 11:16:46 UTC, crimaniak wrote: On Thursday, 13 April 2017 at 05:51:27 UTC, Dukc wrote: auto use(alias F, T)(T t){return F(t);} void main() { import std.stdio; foreach(i; 1 .. 11) { foreach(j; 1 .. 11) write((i * j).use!(x => x*x), " "); writeln; } } This way, you can avoid writing long expressions twice with UFCS. If fact you don't need any template to do this. Try the next: foreach(i; 1 .. 11) { foreach(j; 1 .. 11) write((x => x*x)(i * j), " "); writeln; } Thats a good one, wrote that down for next time. With that I'll share another one I read about that I thought was really cool: import std.stdio; import std.functional: memoize; ulong fib(ulong n) { alias mfib = memoize!(fib); return n < 2 ? 1 : mfib(n-2) + mfib(n-1); } void main() { foreach (x; 1..45) { writefln("%s", x.fib); } } What this does is make calculating the recursive fibonacci sequence much faster. I don't know too much about the technique, but having it in my optimizations.txt is always a good thing. Now that I think of it, I wonder if there is a page on this website that lists common optimization techniques.
Dlang Features You Would Like To Share
What are some features that you have discovered that you would like to share with the community? For me, one thing I found interesting was the ability to define structures dynamically using mixins: import std.stdio; import std.format: format; template MakePoint(string name, string x, string y) { const char[] MakePoint = "struct %s {int %s; int %s;}".format(name, x, y); } mixin(MakePoint!("Point", "x", "y")); void main() { auto pt = new Point; pt.x = 1; pt.y = 2; writefln("point at (%s, %s)", pt.x, pt.y); }
Re: What is this error message telling me?
On Tuesday, 11 April 2017 at 14:51:44 UTC, Anonymous wrote: I was watching a dconf presentation from last year and wanted to try this out: https://github.com/luismarques/parnas72. It doesn't compile / run as it is and the problem seems to be in the function below. import std.algorithm; import std.range; import std.uni; /// Performs [["foo", "bar"], ["baz"]] -> ["baz", "foo bar"] auto alphabetized(Range)(Range range) { return range .map!(line => line.joiner(" ")) .array .sort!((a, b) => icmp(a, b) < 0); } void main() { auto a = alphabetized([["foo", "bar"], ["baz"]]); } More specifically, icmp doesn't seem to be allowed as the predicate for sort: Here's the error message I get: C:\D\dmd2\windows\bin\..\..\src\phobos\std\uni.d(7082): Error: function 'std.algorithm.searching.skipOver!(Result, dstring).skipOver' is not nothrow C:\D\dmd2\windows\bin\..\..\src\phobos\std\uni.d(7055): Error: nothrow function 'std.uni.fullCasedCmp!(Result).fullCasedCmp' may throw C:\D\dmd2\windows\bin\..\..\src\phobos\std\uni.d(7136): Error: template instance std.uni.fullCasedCmp!(Result) error instantiating test.d(14):instantiated from here: icmp!(Result, Result) C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(1851): instantiated from here: __lambda3!(Result, Result) test.d(14):instantiated from here: sort!((a, b) => icmp(a, b) < 0, cast(SwapStrategy)0, Result[]) test.d(19):instantiated from here: alphabetized!(string[][]) C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm\sorting.d(1863): Error: static assert "Invalid predicate passed to sort: __lambda3" test.d(14):instantiated from here: sort!((a, b) => icmp(a, b) < 0, cast(SwapStrategy)0, Result[]) test.d(19):instantiated from here: alphabetized!(string[][]) My question is, how do I begin to understand error messages like the above? I looked at the signature for sort and icmp and don't get what the problem is. Thanks. The following code gives you the output you want: //code starts import std.algorithm: joiner, map, sort, cmp; import std.array: array; auto alpha(T)(T[][] range) { return range .map!(line => line.joiner(" ")) .array .sort!((a,b) => cmp(a, b) < 0); } void main() { import std.stdio; auto a = alpha!string([["foo", "bar"], ["baz"]]); a.writeln; } //code ends I really don't know why your program gave you any errors, but I can tell you my thinking process. Immediately the first thing I noticed was that your function parameters didn't look explicit at all, so I tried to make them more explicit here. Next, i noticed the use of icmp was the problem in the error message. I thought maybe that function was outdated or something, so i decided to use the comparison function in std.algorithm. Third, I used selective importing in case the compiler got confused what functions to use. All and all, for me, I read the error messages from the bottom up. Also I usually use functions found in std.algorithm because they seem for stable for me.
Re: dmd Backend converted to Boost License
On Friday, 7 April 2017 at 15:14:40 UTC, Walter Bright wrote: https://github.com/dlang/dmd/pull/6680 Yes, this is for real! Symantec has given their permission to relicense it. Thank you, Symantec! Very good news and a solid accomplishment for being on top of Hacker News (as of writing this). It's very good when dlang is discussed on the site along with the other trendy languages. It certainly deserves to be within common programming discourse. Also, congratulations on this big accomplishment!
Re: reading from file
On Tuesday, 13 December 2016 at 15:42:16 UTC, Namal wrote: Hello, comming from C++, I find it hard to remember and understand how reading from file should be done in D. Especially since I am not very good in functional programming. So I have a file which looks like this: 1,2,3,4 5,6,7,8 9,11,11,12 and so on How could I read it row by row and create an array accordingly without reading the comma? import std.stdio; import std.string:strip; import std.algorithm: splitter, each; import std.array: array; void main() { //prepare variables File file = File("new.txt", "r"); string[] arr; //read file while(!file.eof) { file .readln .strip .splitter(",") .array .each!(n => arr ~= n); } arr.writeln; } //here is my attempt. copy, paste, run the program to see if it is what you want. //feel free to ask any questions about my code, it isn't perfect but it works.
Re: Linus' idea of "good taste" code
On Tuesday, 25 October 2016 at 22:53:54 UTC, Walter Bright wrote: It's a small bit, but the idea here is to eliminate if conditionals where possible: https://medium.com/@bartobri/applying-the-linus-tarvolds-good-taste-coding-requirement-99749f37684a#.nhth1eo4e This is something we could all do better at. Making code a straight path makes it easier to reason about and test. Eliminating loops is something D adds, and goes even further to making code a straight line. One thing I've been trying to do lately when working with DMD is to separate code that gathers information from code that performs an action. (The former can then be made pure.) My code traditionally has it all interleaved together. Interesting, that's going in my tips.txt file. Quick question, if you don't mind. What would be the top three things you've learned that significantly made you a better programmer?