Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
January 25, 2019 Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
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; } |
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Levi | 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.
|
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | 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.
|
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | 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 |
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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.
|
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | 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 :-)
|
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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 |
January 25, 2019 Re: Inline Operator from Function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Neia Neutuladh | 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.
|
Copyright © 1999-2021 by the D Language Foundation