www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [your code here]

reply "Joshua Niehus" <jm.niehus gmail.com> writes:
Not as fancy as the other submits, but it might be worthy of the 
front page:

import std.stdio, std.traits;

void main(string[] args) {
     auto foo(T)(T n) {
         return delegate(T i) {
             static if (isSomeString!(T))
                 auto m = mixin("n ~ i");
             else
                 auto m = mixin("n + i");
             return m;
         };
     }
     writeln(foo("Hello")(" World!"));
     writeln(foo(18)(24));   }
Feb 17 2012
next sibling parent Jose Armando Garcia <jsancio gmail.com> writes:
On Fri, Feb 17, 2012 at 6:33 PM, Joshua Niehus <jm.niehus gmail.com> wrote:
 Not as fancy as the other submits, but it might be worthy of the front pa=

 import std.stdio, std.traits;

 void main(string[] args) {
 =A0 =A0auto foo(T)(T n) {
 =A0 =A0 =A0 =A0return delegate(T i) {
 =A0 =A0 =A0 =A0 =A0 =A0static if (isSomeString!(T))
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0auto m =3D mixin("n ~ i");
 =A0 =A0 =A0 =A0 =A0 =A0else
 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0auto m =3D mixin("n + i");
 =A0 =A0 =A0 =A0 =A0 =A0return m;
 =A0 =A0 =A0 =A0};
 =A0 =A0}
 =A0 =A0writeln(foo("Hello")(" World!"));
 =A0 =A0writeln(foo(18)(24)); =A0 }

Looks like Haskell to me. Granted that Haskell has syntactic sugar for this that makes it more readable. -Jose
Feb 17 2012
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/17/12 2:33 PM, Joshua Niehus wrote:
 Not as fancy as the other submits, but it might be worthy of the front
 page:

 import std.stdio, std.traits;

 void main(string[] args) {
 auto foo(T)(T n) {
 return delegate(T i) {
 static if (isSomeString!(T))
 auto m = mixin("n ~ i");
 else
 auto m = mixin("n + i");
 return m;
 };
 }
 writeln(foo("Hello")(" World!"));
 writeln(foo(18)(24)); }

Thanks. I have received a proposal in my inbox for rotating examples. Stay tuned. Andrei
Feb 17 2012
prev sibling next sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
18.02.2012 0:33, Joshua Niehus пишет:
 Not as fancy as the other submits, but it might be worthy of the front
 page:

 import std.stdio, std.traits;

 void main(string[] args) {
     auto foo(T)(T n) {
         return delegate(T i) {
             static if (isSomeString!(T))
                 auto m = mixin("n ~ i");
             else
                 auto m = mixin("n + i");
             return m;
         };
     }
     writeln(foo("Hello")(" World!"));
     writeln(foo(18)(24)); }

Some remarks: 0. `string[] args` isn't needed; 1. `delegate` is not needed; 2. `isSomeString!(T)` can be replaced by `isSomeString!T`; 3. `static if` can be replaced by `auto m = mixin(isSomeString!T ? "n ~ i" : "n + i");`; 4. With nnew `=>` syntax function body can be replaced by `return (T i) => mixin(isSomeString!T ? "n ~ i" : "n + i");` or `return (T i) => mixin("n" ~ (isSomeString!T ? '~' : '+') ~ 'i');` So your code can looks like this: --- import std.stdio, std.traits; void main() { auto foo(T)(T n) { return (T i) => mixin("n" ~ (isSomeString!T ? '~' : '+') ~ 'i'); } writeln(foo("Hello")(" World!")); writeln(foo(18)(24)); } --- or even like this: --- import std.stdio, std.traits; void bar(alias foo)() { writeln(foo("Hello")(" World!")); writeln(foo(18)(24)); } void main() { bar!(n => (typeof(n) i) => mixin("n" ~ (isSomeString!(typeof(n)) ? '~' : '+') ~ 'i'))(); } --- IMHO, all these three versions should be on the site grouped somehow (i.e. one can navigate between analogous).
Feb 18 2012
prev sibling parent "Joshua Niehus" <jm.niehus gmail.com> writes:
On Saturday, 18 February 2012 at 08:18:32 UTC, Denis Shelomovskij 
wrote:
 Some remarks:
 0. `string[] args` isn't needed;
 1. `delegate` is not needed;
 2. `isSomeString!(T)` can be replaced by `isSomeString!T`;
 3. `static if` can be replaced by `auto m = 
 mixin(isSomeString!T ? "n ~ i" : "n + i");`;
 4. With nnew `=>` syntax function body can be replaced by
 `return (T i) => mixin(isSomeString!T ? "n ~ i" : "n + i");`
 or
 `return (T i) => mixin("n" ~ (isSomeString!T ? '~' : '+') ~ 
 'i');`


 So your code can looks like this:
 ---
 import std.stdio, std.traits;

 void main() {
     auto foo(T)(T n) {
         return (T i) => mixin("n" ~ (isSomeString!T ? '~' : 
 '+') ~ 'i');
     }
     writeln(foo("Hello")(" World!"));
     writeln(foo(18)(24));
}
 
 IMHO, all these three versions should be on the site grouped 
 somehow (i.e. one can navigate between analogous).

Ahhh, thats much cleaner, thanks.
Feb 18 2012