www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Dlang Features You Would Like To Share

reply bluecat <1bluecat7 gmail.com> writes:
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);
}
Apr 12 2017
next sibling parent Stefan Koch <uplink.coder googlemail.com> writes:
On Wednesday, 12 April 2017 at 21:40:48 UTC, bluecat wrote:
 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);
 }
this one does not need to be a template. You can build the string using ctfe which is much faster indeed.
Apr 12 2017
prev sibling next sibling parent reply Dukc <ajieskola gmail.com> writes:
On Wednesday, 12 April 2017 at 21:40:48 UTC, bluecat wrote:
 What are some features that you have discovered that you would 
 like to share with the community?
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.
Apr 12 2017
next sibling parent reply crimaniak <crimaniak gmail.com> writes:
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; }
Apr 13 2017
next sibling parent bluecat <1bluecat7 gmail.com> writes:
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.
Apr 13 2017
prev sibling parent Dukc <ajieskola gmail.com> writes:
On Thursday, 13 April 2017 at 11:16:46 UTC, crimaniak wrote:
 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;
     }
True, that's a good trick too. But I prefer the template at least sometimes because I ususally construct the argument up first and only then start to think about what function to call. With the use template, I can write and read in order I think -Exactly the reason why UFCS rules in my opinion.
Apr 13 2017
prev sibling parent Dukc <ajieskola gmail.com> writes:
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;
     }
 }
forgot three letters: auto use(alias F, T...)(T t){return F(t);} to make it work with many arguments: void main() { import std.stdio; foreach(i; 1 .. 11) { foreach(j; 1 .. 11) write((i * j).use!((x, y) => x^^y)(2), " "); writeln; } }
Apr 13 2017
prev sibling next sibling parent Guillaume Piolat <first.last gmail.com> writes:
On Wednesday, 12 April 2017 at 21:40:48 UTC, bluecat wrote:
 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);
 }
There are quite a few in https://p0nce.github.io/d-idioms/ and This Week in D: http://arsdnet.net/this-week-in-d/
Apr 13 2017
prev sibling parent Idan Arye <GenericNPC gmail.com> writes:
On Wednesday, 12 April 2017 at 21:40:48 UTC, bluecat wrote:
 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);
 }
I really like the ability to pass delegates as `alias` template arguments. This allows me to pass these delegates as templates, and the "higher-order" template can instantiate them multiple times with multiple types: https://dpaste.dzfl.pl/e2a7a252b5cc
Apr 13 2017