Re: D idioms list

2016-03-08 Thread Muhavare via Digitalmars-d-announce
For optimal AA lookup, this idiom is also nice if you only need 
the result for one line:


  if (auto found = key in AA)
do_stuff (found);

http://idioms.in/


Re: D idioms list

2015-01-14 Thread Vlad Levenfeld via Digitalmars-d-announce
For optimal AA lookup, this idiom is also nice if you only need 
the result for one line:


  if (auto found = key in AA)
do_stuff (found);


Re: D idioms list

2015-01-11 Thread Russel Winder via Digitalmars-d-announce

On Sat, 2015-01-10 at 14:13 -0800, Walter Bright via Digitalmars-d-announce 
wrote:
 On 1/10/2015 1:28 PM, weaselcat wrote:
  Sorry for the off-topic noise, but where will you be publishing 
  your articles
  since Dr.Dobbs has closed?
  
  Sorry if you have answered this elsewhere.
 
 It's a good question. Dr. Dobb's has graciously given me permission 
 to republish
 them, and I'll post them on http://digitalmars.com/articles. As you 
 can see,
 I've already done a few of them. Lots more to go.

Feel free to send stuff to ACCU's CVu or Overload. 

http://accu.org/index.php/journal


-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



Re: D idioms list

2015-01-11 Thread Walter Bright via Digitalmars-d-announce

On 1/11/2015 4:24 AM, Russel Winder via Digitalmars-d-announce wrote:

Feel free to send stuff to ACCU's CVu or Overload.

http://accu.org/index.php/journal



Good idea!


Re: D idioms list

2015-01-10 Thread Walter Bright via Digitalmars-d-announce

On 1/10/2015 1:28 PM, weaselcat wrote:

Sorry for the off-topic noise, but where will you be publishing your articles
since Dr.Dobbs has closed?

Sorry if you have answered this elsewhere.


It's a good question. Dr. Dobb's has graciously given me permission to republish 
them, and I'll post them on http://digitalmars.com/articles. As you can see, 
I've already done a few of them. Lots more to go.


Re: D idioms list

2015-01-10 Thread weaselcat via Digitalmars-d-announce

On Saturday, 10 January 2015 at 20:37:04 UTC, Walter Bright wrote:

On 1/8/2015 2:21 AM, ponce wrote:

I've started a list of curated D tips and tricks here:
http://p0nce.github.io/d-idioms/

Anything that you wished you learned earlier at one point in 
the D world is

welcome to be added or suggested.



My contribution:

http://digitalmars.com/articles/b68.html

(Member function pointers in D)


Sorry for the off-topic noise, but where will you be publishing 
your articles since Dr.Dobbs has closed?


Sorry if you have answered this elsewhere.


Re: D idioms list

2015-01-10 Thread Walter Bright via Digitalmars-d-announce

On 1/8/2015 2:21 AM, ponce wrote:

I've started a list of curated D tips and tricks here:
http://p0nce.github.io/d-idioms/

Anything that you wished you learned earlier at one point in the D world is
welcome to be added or suggested.



My contribution:

http://digitalmars.com/articles/b68.html

(Member function pointers in D)


Re: D idioms list

2015-01-09 Thread ponce via Digitalmars-d-announce
On Friday, 9 January 2015 at 05:58:09 UTC, ketmar via 
Digitalmars-d-announce wrote:
p.p.s. maybe it's worth adding Artur's code sample[1] too, to 
show that
extended structure can be passed to functions which requires 
original

one? it's not obvious, at least for me. ;-)

[1] 
http://forum.dlang.org/post/mailman.4332.1420752329.9932.digitalmars-d-annou...@puremagic.com


I didn't knew alias this does object slicing. Will add it.


Re: D idioms list

2015-01-08 Thread Foo via Digitalmars-d-announce
I saw recently (at last in this thread: 
http://forum.dlang.org/thread/tdfydchrairigdlgt...@forum.dlang.org#post-qakiogaqvmiwlneimhgu:40forum.dlang.org) 
that many users use


key in aa ? aa[key] : ValueType.init;

instead of

auto ptr = key in aa;
ptr ? *ptr : ValueType.init;

which is more economic.
Maybe you can add it to your list:


import std.stdio;

void main() {
immutable string key = foo;
immutable string[string] arr = [key : bar];

if (auto ptr = key in arr)
writeln(*ptr);
}



Re: D idioms list

2015-01-08 Thread ketmar via Digitalmars-d-announce
On Thu, 08 Jan 2015 21:22:30 +
ponce via Digitalmars-d-announce digitalmars-d-announce@puremagic.com
wrote:

 On Thursday, 8 January 2015 at 20:23:11 UTC, ketmar via 
 Digitalmars-d-announce wrote:
  i'm not sure, but maybe it worth renaming struct inheritance 
  to
  extending a struct? or even something completely different. 
  what it
  does is actually extending/augmenting the struct, but not
  OO-inheritance, as one cannot pass augmented struct to the 
  function
  which expects original struct. at least without hackery.
 
 Renamed, thanks!
we actually can pass extended struct as original one, as Artur shown,
but i believe that extending is still better.

p.s. you forgot to fix TOC, which still reads struct inheritance.


signature.asc
Description: PGP signature


Re: D idioms list

2015-01-08 Thread ketmar via Digitalmars-d-announce
On Thu, 08 Jan 2015 22:25:11 +0100
Artur Skawina via Digitalmars-d-announce
digitalmars-d-announce@puremagic.com wrote:

 On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:
  i'm not sure, but maybe it worth renaming struct inheritance to
  extending a struct? or even something completely different. what it
  does is actually extending/augmenting the struct, but not
  OO-inheritance, as one cannot pass augmented struct to the function
  which expects original struct. at least without hackery.
 
 'alias this' is just the D syntax for implicit conversions.
 The feature /is/ crippled, but there's no need for hackery;
 at least not for simple things like that.
 
struct A { int a; }
struct B { A a; alias a this; string b; }
 
int f(A a) { return a.a+1; }
int g(ref A a) { return a.a+1; }
ref A h(ref A a) { return a; }
 
int main() {
   B b;
   return f(b)+g(b)+h(b).a;
}
 
 artur
mea culpa. i completely forgot about that feature of `alias this`, and
was pretty sure that the code above is invalid. i never bothered to
really check it. sorry.


signature.asc
Description: PGP signature


Re: D idioms list

2015-01-08 Thread ketmar via Digitalmars-d-announce
On Thu, 08 Jan 2015 21:22:30 +
ponce via Digitalmars-d-announce digitalmars-d-announce@puremagic.com
wrote:

 On Thursday, 8 January 2015 at 20:23:11 UTC, ketmar via 
 Digitalmars-d-announce wrote:
  i'm not sure, but maybe it worth renaming struct inheritance 
  to
  extending a struct? or even something completely different. 
  what it
  does is actually extending/augmenting the struct, but not
  OO-inheritance, as one cannot pass augmented struct to the 
  function
  which expects original struct. at least without hackery.
 
 Renamed, thanks!
p.p.s. maybe it's worth adding Artur's code sample[1] too, to show that
extended structure can be passed to functions which requires original
one? it's not obvious, at least for me. ;-)

[1] 
http://forum.dlang.org/post/mailman.4332.1420752329.9932.digitalmars-d-annou...@puremagic.com


signature.asc
Description: PGP signature


Re: D idioms list

2015-01-08 Thread weaselcat via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
I've started a list of curated D tips and tricks here: 
http://p0nce.github.io/d-idioms/


Anything that you wished you learned earlier at one point in 
the D world is welcome to be added or suggested.


I think the focus should be on stuff that could make you more 
productive, or is just funky but that is up to debate.


Of course the D Cookbook still stays irreplaceable for a 
consistent, in-depth discussion of being D-enabled.


Thoughts?


Not much to add but I enjoy reading 'idiomatic' D content - 
coming from C++, I feel like I'm often not writing my D code like 
I should be. Thanks for the extra resource.


Re: D idioms list

2015-01-08 Thread Szymon Gatner via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
I've started a list of curated D tips and tricks here: 
http://p0nce.github.io/d-idioms/


Anything that you wished you learned earlier at one point in 
the D world is welcome to be added or suggested.


I think the focus should be on stuff that could make you more 
productive, or is just funky but that is up to debate.


Of course the D Cookbook still stays irreplaceable for a 
consistent, in-depth discussion of being D-enabled.


Thoughts?


They are really cool, thanks :)

Question:

Where did this syntax came from? It is not documented for 
'import' keyword.(first time I see that D has built-in resource 
compiler):


ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);


Re: D idioms list

2015-01-08 Thread ketmar via Digitalmars-d-announce
On Thu, 08 Jan 2015 11:24:34 +
Szymon Gatner via Digitalmars-d-announce
digitalmars-d-announce@puremagic.com wrote:

 On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
  I've started a list of curated D tips and tricks here: 
  http://p0nce.github.io/d-idioms/
 
  Anything that you wished you learned earlier at one point in 
  the D world is welcome to be added or suggested.
 
  I think the focus should be on stuff that could make you more 
  productive, or is just funky but that is up to debate.
 
  Of course the D Cookbook still stays irreplaceable for a 
  consistent, in-depth discussion of being D-enabled.
 
  Thoughts?
 
 They are really cool, thanks :)
 
 Question:
 
 Where did this syntax came from? It is not documented for 
 'import' keyword.(first time I see that D has built-in resource 
 compiler):
 
 ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);
it is documented: http://dlang.org/expression.html#ImportExpression
it's a nice D habit of overloading keywords.


signature.asc
Description: PGP signature


Re: D idioms list

2015-01-08 Thread ponce via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:56:00 UTC, bearophile wrote:

ponce:

I'm not familiar with the terse, range-heavy, UFCS style that 
has emerged from Phobos


In Rosettacode I have inserted tons of examples of that coding 
style.


An example, given a tuple of arbitrary length, with items all 
of the same type, how do you compute the total of its items?


The last way I've invented is:

myTuple[].only.sum

It's also @nogc. But it causes a little of template bloat.

Bye,
bearophile


Cool. I will link to the Rosettacode D pages since I've used them 
in the past when time-constrained, especially all things 
regarding text files.


Re: D idioms list

2015-01-08 Thread Szymon Gatner via Digitalmars-d-announce
On Thursday, 8 January 2015 at 11:31:14 UTC, ketmar via 
Digitalmars-d-announce wrote:

On Thu, 08 Jan 2015 11:24:34 +
Szymon Gatner via Digitalmars-d-announce
digitalmars-d-announce@puremagic.com wrote:


On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
 I've started a list of curated D tips and tricks here: 
 http://p0nce.github.io/d-idioms/


 Anything that you wished you learned earlier at one point in 
 the D world is welcome to be added or suggested.


 I think the focus should be on stuff that could make you 
 more productive, or is just funky but that is up to debate.


 Of course the D Cookbook still stays irreplaceable for a 
 consistent, in-depth discussion of being D-enabled.


 Thoughts?

They are really cool, thanks :)

Question:

Where did this syntax came from? It is not documented for 
'import' keyword.(first time I see that D has built-in 
resource compiler):


ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);
it is documented: 
http://dlang.org/expression.html#ImportExpression

it's a nice D habit of overloading keywords.


Ah, thanks. Follow up then: can such imported string be used for 
mixin?


Re: D idioms list

2015-01-08 Thread ponce via Digitalmars-d-announce

On Thursday, 8 January 2015 at 11:41:43 UTC, Szymon Gatner wrote:


Question:

Where did this syntax came from? It is not documented for 
'import' keyword.(first time I see that D has built-in 
resource compiler):


ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);
it is documented: 
http://dlang.org/expression.html#ImportExpression

it's a nice D habit of overloading keywords.


Ah, thanks. Follow up then: can such imported string be used 
for mixin?


Yes.



Re: D idioms list

2015-01-08 Thread Szymon Gatner via Digitalmars-d-announce

On Thursday, 8 January 2015 at 11:43:30 UTC, ponce wrote:
On Thursday, 8 January 2015 at 11:41:43 UTC, Szymon Gatner 
wrote:


Question:

Where did this syntax came from? It is not documented for 
'import' keyword.(first time I see that D has built-in 
resource compiler):


ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);
it is documented: 
http://dlang.org/expression.html#ImportExpression

it's a nice D habit of overloading keywords.


Ah, thanks. Follow up then: can such imported string be used 
for mixin?


Yes.


That is pretty damn cool then.


Re: D idioms list

2015-01-08 Thread ketmar via Digitalmars-d-announce
On Thu, 08 Jan 2015 11:41:42 +
Szymon Gatner via Digitalmars-d-announce
digitalmars-d-announce@puremagic.com wrote:

 On Thursday, 8 January 2015 at 11:31:14 UTC, ketmar via 
 Digitalmars-d-announce wrote:
  On Thu, 08 Jan 2015 11:24:34 +
  Szymon Gatner via Digitalmars-d-announce
  digitalmars-d-announce@puremagic.com wrote:
 
  On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
   I've started a list of curated D tips and tricks here: 
   http://p0nce.github.io/d-idioms/
  
   Anything that you wished you learned earlier at one point in 
   the D world is welcome to be added or suggested.
  
   I think the focus should be on stuff that could make you 
   more productive, or is just funky but that is up to debate.
  
   Of course the D Cookbook still stays irreplaceable for a 
   consistent, in-depth discussion of being D-enabled.
  
   Thoughts?
  
  They are really cool, thanks :)
  
  Question:
  
  Where did this syntax came from? It is not documented for 
  'import' keyword.(first time I see that D has built-in 
  resource compiler):
  
  ubyte[] sdlBytes = cast(ubyte[]) import(SDL2.dll);
  it is documented: 
  http://dlang.org/expression.html#ImportExpression
  it's a nice D habit of overloading keywords.
 
 Ah, thanks. Follow up then: can such imported string be used for 
 mixin?
sure. either directly, or you can use CTFE to parse imported data and
generate code. for now it's somewhat limited, 'cause CTFE parsing eats
alot of memory, but when we'll have 128GB of RAM at bare minimum... i
don't think that i'll be using external preprocessors to generate D
code from various text and binary files.


signature.asc
Description: PGP signature


Re: D idioms list

2015-01-08 Thread ponce via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:30:38 UTC, uri wrote:


This is great, thanks.

Something I personally would find useful is a comparison 
between the C++ way and idiomatic D with Phobos. I finding 
coming from C/C++ to D very easy but I'm always wondering if 
I'm doing things the D way.


Cheers,
uri


I'm not familiar with the terse, range-heavy, UFCS style that has 
emerged from Phobos so I'm not sure if I can write that.


What could help is a list of tasks for which you asked yourself 
what the D way was. Is there one?


D idioms list

2015-01-08 Thread ponce via Digitalmars-d-announce
I've started a list of curated D tips and tricks here: 
http://p0nce.github.io/d-idioms/


Anything that you wished you learned earlier at one point in the 
D world is welcome to be added or suggested.


I think the focus should be on stuff that could make you more 
productive, or is just funky but that is up to debate.


Of course the D Cookbook still stays irreplaceable for a 
consistent, in-depth discussion of being D-enabled.


Thoughts?


Re: D idioms list

2015-01-08 Thread uri via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
I've started a list of curated D tips and tricks here: 
http://p0nce.github.io/d-idioms/


Anything that you wished you learned earlier at one point in 
the D world is welcome to be added or suggested.


I think the focus should be on stuff that could make you more 
productive, or is just funky but that is up to debate.


Of course the D Cookbook still stays irreplaceable for a 
consistent, in-depth discussion of being D-enabled.


Thoughts?


This is great, thanks.

Something I personally would find useful is a comparison between 
the C++ way and idiomatic D with Phobos. I finding coming from 
C/C++ to D very easy but I'm always wondering if I'm doing things 
the D way.


Cheers,
uri


Re: D idioms list

2015-01-08 Thread uri via Digitalmars-d-announce

On Thursday, 8 January 2015 at 10:35:07 UTC, ponce wrote:

On Thursday, 8 January 2015 at 10:30:38 UTC, uri wrote:


This is great, thanks.

Something I personally would find useful is a comparison 
between the C++ way and idiomatic D with Phobos. I finding 
coming from C/C++ to D very easy but I'm always wondering if 
I'm doing things the D way.


Cheers,
uri


I'm not familiar with the terse, range-heavy, UFCS style that 
has emerged from Phobos so I'm not sure if I can write that.


What could help is a list of tasks for which you asked yourself 
what the D way was. Is there one?


No I admit I don't have any real list. It's always an in the 
moment sort of thing and I then just choose a D-ish/C++ style 
and promptly forget the exact details.


I'll start to compile a list each time this comes up. And if I 
find any good D idioms in the process I'll include them in the 
list as well.


Thanks,
uri


Re: D idioms list

2015-01-08 Thread bearophile via Digitalmars-d-announce

ponce:

I'm not familiar with the terse, range-heavy, UFCS style that 
has emerged from Phobos


In Rosettacode I have inserted tons of examples of that coding 
style.


An example, given a tuple of arbitrary length, with items all of 
the same type, how do you compute the total of its items?


The last way I've invented is:

myTuple[].only.sum

It's also @nogc. But it causes a little of template bloat.

Bye,
bearophile


Re: D idioms list

2015-01-08 Thread Robert burner Schadek via Digitalmars-d-announce

that a really nice idea, thanks.

substring position, std.string.(last)indexOf(|Any|Neither) may be 
better



btw. this should move to the dlang wiki. Any takers?


Re: D idioms list

2015-01-08 Thread Foo via Digitalmars-d-announce

On Thursday, 8 January 2015 at 20:00:11 UTC, Foo wrote:

On Thursday, 8 January 2015 at 10:21:26 UTC, ponce wrote:
I've started a list of curated D tips and tricks here: 
http://p0nce.github.io/d-idioms/


Anything that you wished you learned earlier at one point in 
the D world is welcome to be added or suggested.


I think the focus should be on stuff that could make you more 
productive, or is just funky but that is up to debate.


Of course the D Cookbook still stays irreplaceable for a 
consistent, in-depth discussion of being D-enabled.


Thoughts?


Struct inheritance with alias this
You are using a class ;)


And the public label is redundant.


Re: D idioms list

2015-01-08 Thread Artur Skawina via Digitalmars-d-announce
On 01/08/15 21:23, ketmar via Digitalmars-d-announce wrote:
 i'm not sure, but maybe it worth renaming struct inheritance to
 extending a struct? or even something completely different. what it
 does is actually extending/augmenting the struct, but not
 OO-inheritance, as one cannot pass augmented struct to the function
 which expects original struct. at least without hackery.

'alias this' is just the D syntax for implicit conversions.
The feature /is/ crippled, but there's no need for hackery;
at least not for simple things like that.

   struct A { int a; }
   struct B { A a; alias a this; string b; }

   int f(A a) { return a.a+1; }
   int g(ref A a) { return a.a+1; }
   ref A h(ref A a) { return a; }

   int main() {
  B b;
  return f(b)+g(b)+h(b).a;
   }

artur