www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inline Operator from Function.

reply Jonathan Levi <catanscout gmail.com> writes:
I know this was not an intentional feature of d, but it is a cool 
quirk.

If you want to use a function that takes two arguments like a 
infix binary operator (similar to the back ticks in Haskell 
"`fun`") you can but a "." before and a "=" after.

     c = (a .fun= b);

Example of it working (https://run.dlang.io/is/rr7ChO)

     import std.stdio;

     void main() {
         writeln(5 .mul= 2);
     }

     int mul(int a, int b) {
     	return a*b;
     }
Jan 24 2019
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Fri, 25 Jan 2019 03:45:36 +0000, Jonathan Levi wrote:
 I know this was not an intentional feature of d, but it is a cool quirk.
 
 If you want to use a function that takes two arguments like a infix
 binary operator (similar to the back ticks in Haskell "`fun`") you can
 but a "." before and a "=" after.
 
      c = (a .fun= b);
Consider also: struct Operator(alias fn) if (Parameters!fn.length == 2) { Curry opBinaryRight(string op)(Parameters!fn[0] arg) if (op == "/") { return Curry(arg); } struct Curry { Parameters!fn[0] arg; auto opBinaryRight(string op)(Parameters!fn[1] arg2) if (op == "/") { return fn(arg, arg2); } } } To use it: alias min = Operator!((a, b) => a < b ? a : b); writeln(1 /min/ 2); Credit to FeepingCreature for inventing this 5-10 years ago.
Jan 24 2019
next sibling parent Neia Neutuladh <neia ikeran.org> writes:
On Fri, 25 Jan 2019 03:52:23 +0000, Neia Neutuladh wrote:
 alias min = Operator!((a, b) => a < b ? a : b);
Er, that should obviously be: enum min = Operator!(...).init; Or use a static function for the operator overload. It's a land of lawlessness here, do whatever you like.
Jan 24 2019
prev sibling next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 25 January 2019 at 03:52:23 UTC, Neia Neutuladh wrote:

 Credit to FeepingCreature for inventing this 5-10 years ago.
2007 it seems, assuming FeepingCreature == downs: https://forum.dlang.org/post/fg15ev$12uh$1 digitalmars.com
Jan 24 2019
parent reply Neia Neutuladh <neia ikeran.org> writes:
On Fri, 25 Jan 2019 05:04:41 +0000, Mike Parker wrote:
 On Friday, 25 January 2019 at 03:52:23 UTC, Neia Neutuladh wrote:
 
 Credit to FeepingCreature for inventing this 5-10 years ago.
2007 it seems, assuming FeepingCreature == downs: https://forum.dlang.org/post/fg15ev$12uh$1 digitalmars.com
Apologies for the misattribution, and thank you for correcting me.
Jan 24 2019
parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 25 January 2019 at 05:20:37 UTC, Neia Neutuladh wrote:
 On Fri, 25 Jan 2019 05:04:41 +0000, Mike Parker wrote:
 On Friday, 25 January 2019 at 03:52:23 UTC, Neia Neutuladh 
 wrote:
 
 Credit to FeepingCreature for inventing this 5-10 years ago.
2007 it seems, assuming FeepingCreature == downs: https://forum.dlang.org/post/fg15ev$12uh$1 digitalmars.com
Apologies for the misattribution, and thank you for correcting me.
I wasn't correcting, but genuinely curious if it was the same person :-)
Jan 25 2019
parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 25 January 2019 at 08:51:55 UTC, Mike Parker wrote:
 On Friday, 25 January 2019 at 05:20:37 UTC, Neia Neutuladh 
 wrote:
 On Fri, 25 Jan 2019 05:04:41 +0000, Mike Parker wrote:
 On Friday, 25 January 2019 at 03:52:23 UTC, Neia Neutuladh 
 wrote:
 
 Credit to FeepingCreature for inventing this 5-10 years ago.
2007 it seems, assuming FeepingCreature == downs: https://forum.dlang.org/post/fg15ev$12uh$1 digitalmars.com
Apologies for the misattribution, and thank you for correcting me.
I wasn't correcting, but genuinely curious if it was the same person :-)
From https://forum.dlang.org/post/imqv4k$7kv$2 digitalmars.com: On Monday, 28 March 2011 at 21:38:29 UTC, FeepingCreature wrote:
 I was d|owns but that name was stupid.
-- Simen
Jan 25 2019
prev sibling parent bauss <jj_1337 live.dk> writes:
On Friday, 25 January 2019 at 03:52:23 UTC, Neia Neutuladh wrote:
 On Fri, 25 Jan 2019 03:45:36 +0000, Jonathan Levi wrote:
 I know this was not an intentional feature of d, but it is a 
 cool quirk.
 
 If you want to use a function that takes two arguments like a 
 infix binary operator (similar to the back ticks in Haskell 
 "`fun`") you can but a "." before and a "=" after.
 
      c = (a .fun= b);
Consider also: struct Operator(alias fn) if (Parameters!fn.length == 2) { Curry opBinaryRight(string op)(Parameters!fn[0] arg) if (op == "/") { return Curry(arg); } struct Curry { Parameters!fn[0] arg; auto opBinaryRight(string op)(Parameters!fn[1] arg2) if (op == "/") { return fn(arg, arg2); } } } To use it: alias min = Operator!((a, b) => a < b ? a : b); writeln(1 /min/ 2); Credit to FeepingCreature for inventing this 5-10 years ago.
Now that's a hack I can support.
Jan 25 2019