www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Pushing D's mixin to the limits: Project Euler Problem 61 from Ruby

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
http://rounin.livejournal.com/24639.html

http://www.reddit.com/r/programming/comments/27zjd5/pushing_ds_mixin_to_the_limits_project_euler/

https://www.facebook.com/dlang.org/posts/864930913520591

https://twitter.com/D_Programming/status/477162603140374528


Andrei
Jun 12 2014
parent reply Philippe Sigaud via Digitalmars-d-announce writes:
Drat, I need a livejournal account to post comments? Well, I can at
least create a reddit account, I guess...

"In the Ruby version, I was able to add the methods directly to the
Integer class, using what's known as monkey patching, allowing me to
make calls like3.pentagonal. In D, the methods are global instead, so
the call would be pentagonal(3)."

Hmm, 3.pentagonal is valid D code. If that makes it more palatable to
Rubyists, that's good news.

There is something I don't get:

"The doublemixins with the method calls also took a while to figure
out, after the convenience of eval"

That is:

mixin("mixin(defineMain());");

I don't understand why he's doing that. AFAICT, simply using

mixin(defineMain());

would work perfectly, no? (defineMain and his other functions return strings).
Jun 12 2014
parent reply "Rounin" <david 01101001.net> writes:
Hey there!

Yeah, to expect people to register on LiveJournal in this age of 
Facebook... Sorry about that; It must have been to deter the 
spammers.

Thanks for taking the time to comment! Your solution with the 
single call to mixin() is much more elegant. I made a version 2 
which uses it.

The reason I used a double mixin() was that the first thing I 
tried was mixin("foreach() etc. etc."), which I think may have 
failed due to the foreach, then mixin("defineMain();"); , which 
was interpreted as a function declaration, and then I went 
straight for the mixin("mixin()"); . If it ain't broke, et cetera.

I still think the double mixin() can compete in terms of comedy 
value, though, don't you think?

Also, thanks for pointing out UFCS. It seems like a very 
convenient, not to mention SANE alternative to monkey patching, 
and it'll make it even more seamless to port code that makes use 
of that mechanism.
Jun 12 2014
next sibling parent "Dicebot" <public dicebot.lv> writes:
On Thursday, 12 June 2014 at 20:35:39 UTC, Rounin wrote:
 I still think the double mixin() can compete in terms of comedy 
 value, though, don't you think?
That was actually my initial guess when having a quick look trough the code samples :) "true D programmers mixin their mixins"
Jun 12 2014
prev sibling next sibling parent Philippe Sigaud via Digitalmars-d-announce writes:
On Thu, Jun 12, 2014 at 10:35 PM, Rounin via Digitalmars-d-announce
<digitalmars-d-announce puremagic.com> wrote:
 Hey there!
Oh cool, thanks for answering!
 Yeah, to expect people to register on LiveJournal in this age of Facebook...
 Sorry about that; It must have been to deter the spammers.
Sorry for whining about it. I'm not on Facebook either, I just find it a bit bothersome to have to register every time I want to let a comment. I suppose I'm too used to always commenting in the same places and am leery to getting outside my comfort zones :-)
 Thanks for taking the time to comment! Your solution with the single call to
 mixin() is much more elegant. I made a version 2 which uses it.

 The reason I used a double mixin() was that the first thing I tried was
 mixin("foreach() etc. etc."), which I think may have failed due to the
 foreach, then mixin("defineMain();"); , which was interpreted as a function
 declaration, and then I went straight for the mixin("mixin()"); . If it
 ain't broke, et cetera.

 I still think the double mixin() can compete in terms of comedy value,
 though, don't you think?
mixin("..."); pastes the inside code where the mixin is. So there is no real reason to mix a pure string like mixin("defineMain()"), since you could directly write it into your code. As for foreach, I guess it's because foreach is not a declaration and you cannot put it at the module level: module foo; foreach(i; 0..10) {} is illegal code, I suppose. Mixin or not. In fact, you'll probably never see mixin("some predefined code"); what is used is mixin("some code" ~ some external value ~ "more code"); or mixin(foo(args)); where foo returns a string.
 Also, thanks for pointing out UFCS. It seems like a very convenient, not to
 mention SANE alternative to monkey patching, and it'll make it even more
 seamless to port code that makes use of that mechanism.
Cool! Again, thanks for an interesting article.
Jun 12 2014
prev sibling next sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Thursday, 12 June 2014 at 20:35:39 UTC, Rounin wrote:
 Hey there!

 Yeah, to expect people to register on LiveJournal in this age 
 of Facebook... Sorry about that; It must have been to deter the 
 spammers.
I don't leave comments if it is run through facebook, maybe one day. There should be settings in LJ to allow anonymous comments. Its what I've done (I also turned off approval since I've had good comments come in and no spam).
Jun 12 2014
prev sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
Another hint:

You should return an `int[2]` from `abc()` instead of `int[]`. 
It's faster and doesn't require a heap allocation.
Jun 13 2014