| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
February 17, 2012 [your code here] | ||||
|---|---|---|---|---|
| ||||
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)); }
| ||||
February 17, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Joshua Niehus | 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 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)); }
Looks like Haskell to me. Granted that Haskell has syntactic sugar for this that makes it more readable.
-Jose
| |||
February 17, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Joshua Niehus | 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
| |||
February 18, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Joshua Niehus | 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).
| |||
February 19, 2012 Re: [your code here] | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Denis Shelomovskij | 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.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply