June 22, 2017
On Thursday, 22 June 2017 at 08:52:27 UTC, qznc wrote:
> On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
>> [...]
>
> H. S. Teoh calendar: https://wiki.dlang.org/Component_programming_with_ranges
>
> [...]

Yes that's a great example, but sadly not standalone - we can only use Phobos for the example snippets on dlang.org ...

The examples need to compile and run, so that a reader can modify and play with them.
June 22, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> I would love to hear about your favorite feature(s) in D.

 Arrays include length along with the pointer.
 Very easy built-in array support with foreach
 Ranges, that don't treat their input types as 'pointers' and get all levels of confusion, including that you'd have to include two 'maybe pointers'.
 Constraints on templates, making limiting and specializing very easy.
 static if & version removing the pre-processor...
June 22, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Hi,
>
> I am currently trying to modernize the D code example roulette on the dlang.org front page [1]. Hence, I would love to hear about your favorite feature(s) in D.
> Ideas:
> - favorite language construct
> - favorite code sample
> - "only possible in D"
>
> Before you ask, yes - I want to add a couple of cool examples to dlang.org (and yep the roulette rotation is currently broken [2]).
>
> [1] https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
> [2] https://github.com/dlang/dlang.org/pull/1757

On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:


Universal Function Call Syntax ( UFCS )

Sample code on my gitgub + screen-shot,

https://github.com/k-five/dren

main code: recursively renaming files based on Regexes

dirEntries( ".", ( args[ 4 ] == "-r" ? SpanMode.depth : SpanMode.shallow ), false )
	.filter!( file => !file.name.matchFirst( regex( args[ 1 ] ) ).empty() )
	.filter!( file => ( args[ 3 ] == "-f" || args[ 3 ] == "-d"  ? ( args[ 3 ] == "-f" ? !file.isDir : !file.isFile ) : ( !file.isSymlink ) ) )
	.map!( file => file.name )
        .each!( ( string result ) => ( args[ 5 ] == "-y" ? rename( result, replaceFirst( result, regex( args[ 1 ] ), args[ 2 ] ) ) : writeln( "print: ",result, " >> ", replaceFirst( result, regex( args[ 1 ] ), "\033[1;32m" ~ args[ 2 ] ~ "\033[m" ) ) ) );
June 22, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Hi,
>
> I am currently trying to modernize the D code example roulette on the dlang.org front page [1]. Hence, I would love to hear about your favorite feature(s) in D.
> Ideas:
> - favorite language construct

I think "alias this" is a good contender.
But actually I like how D deal with names.
alias and alias template parameters exposes the identifier, being a type or a value. It is very clean.
June 22, 2017
> Thanks a lot, but I was looking for small snippets (< 10 LoC) than could be used to show the awesomeness of D at the roulette on https://dlang.org
> Any chance you could make your point a bit conciser? :)

I agree with you, indeed it is much too long for a carousel ;)

And it should be made even simpler.

But I still think that the first and only sample code someone should read when coming for the first time on the D website should be this kind of example, which shows how simple and straightforward the D code looks like.

It's very important to reassure the visitor curious about D on how easy it is to learn and use D, showing him that D's syntax is familiar, and the code is very readable and concise.

Some first-time visitors may be meta-programming experts, but many others (including me) are just looking for a simpler alternative to existing languages, which allows them to quickly program their stuff in an easy and efficient way.

And for those looking for a language which allows to program complicated things in a simpler way, as D indeed allows to do it, this should also be shown. But I don't think that this should be the first example someone sees.

That said, I'm not totally against code carousel.

The one on the Python website is very well done.

https://www.python.org/

Its code snippets are very simple on purpose.

Python's landing webpage is very encouraging, and shows that this scripting language can be picked up quickly.

June 22, 2017
On Thursday, 22 June 2017 at 07:00:17 UTC, Seb wrote:
>
> Thanks a lot, but I was looking for small snippets (< 10 LoC) than could be used to show the awesomeness of D at the roulette


enum E {IN = -1, V1, V2, V3, X1 }

mixin({
  string code = "enum EBIT : ulong { "~
                        "init = 0,";      /* We set the dummy init value to 0 */
  foreach(Code; __traits(allMembers, E)) {
    static if(Code == "IN")
      code ~= "INVALID = -1,";
    else
      code ~= Code~"= 1 << E."~Code~",";
  }
  code ~= "
    ALL    =  -1,
  return code ~ "}";
}());

Generates an enum which values depend on the values of another enum.

In C or other languages I would needed to write the 2nd enum myself

enum EBIT { INVALID = -1,
             V1= 1 << E.V1,
             V2= 1 << E.V2,
             V3= 1 << E.V3,
             X1= 1 << E.X1,
          }

For a 4 value enum it doesn't look such a big deal but in my real project coming from C, the first enum holds more than 50 regular values and 3 special values. When I have to add or remove a value I have to edit the first enum, but also the 2nd enum, 5 derived lookup tables and 2 functions using a big switch/case with the enum values.
In D I edit only the first enum, the rest is auto-generated.

As 2nd trick/nice thing here is a function geenrating a switch/case with the enums.

string E2thing(E val)
{
  switch(val) {
    foreach(e; __traits(allMembers, E))
    static if(e != "IN")
      mixin(`case E.`~e~`: return "`~e~`";`);
    default: return "UNDEFINED";
  }
}


June 22, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Hi,
>
> I am currently trying to modernize the D code example roulette on the dlang.org front page [1]. Hence, I would love to hear about your favorite feature(s) in D.
> Ideas:
> - favorite language construct
> - favorite code sample
> - "only possible in D"
>
> Before you ask, yes - I want to add a couple of cool examples to dlang.org (and yep the roulette rotation is currently broken [2]).
>
> [1] https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
> [2] https://github.com/dlang/dlang.org/pull/1757

My favorite features:
- CFTE
- UDA
- delegates
- UFCS
- property
- Template mixins
- Contracts and unittests
June 22, 2017
UDA snippet:
> class MyView : View {
>     @ViewWidget Button okButton;
>     @ViewWidget("cancelButton") Button myButton;
>     @GroupViewWidgets Button[3] buttons;
> 
>     @OnClickListener("okButton")
>     void onOkButtonClick(Widget widget) {
>         ...
>     }
> 
>     @Shortcut("TestGroup.cancel")
>     void someShortcutAction() {
>         ...
>     }
> 
>     @OnClickListener("closeButton")
>     @OnClickListener("cancelButton")
>     void onCancelButtonClick(Widget widget) {
>         ...
>     }
> }

June 23, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Before you ask, yes - I want to add a couple of cool examples to dlang.org (and yep the roulette rotation is currently broken [2]).
>
> [1] https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
> [2] https://github.com/dlang/dlang.org/pull/1757

Is it to mirror python examples? Shouldn't there be simple examples for newbie programmers too?
June 23, 2017
On Thursday, 22 June 2017 at 00:48:25 UTC, Seb wrote:
> Hi,
>
> I am currently trying to modernize the D code example roulette on the dlang.org front page [1]. Hence, I would love to hear about your favorite feature(s) in D.
> Ideas:
> - favorite language construct
> - favorite code sample
> - "only possible in D"
>
> Before you ask, yes - I want to add a couple of cool examples to dlang.org (and yep the roulette rotation is currently broken [2]).
>
> [1] https://github.com/dlang/dlang.org/pulls?q=is%3Apr+is%3Aopen+label%3A%22Frontpage+example%22
> [2] https://github.com/dlang/dlang.org/pull/1757

Fairly specific, but foreach (member; someStruct.tupleof).

Part of my work on my IRC bot has been to serialise structs into configuration files (since std.json was *non-trivial* to deal with), and foreaching a someStruct.tupleof or a __traits(allMembers, symbol) allows for really, really interesting stuff.