Tuple DIP

Mark smarksc at gmail.com
Sat Jan 13 10:47:37 UTC 2018


On Saturday, 13 January 2018 at 00:51:51 UTC, Timon Gehr wrote:
> On 13.01.2018 01:20, Mark wrote:
>> int (x,y) = f(1, 2); // x=3, y=-1
>> int (w,z) = (1, 2).f() // same as above, UFCS
>> int (u,v) = (1, 2).(sum, diff) // same as above, "lambda tuple"
>> 
>> In the last example, (sum, diff) is basically lowered to (x,y) 
>> => (sum(x,y), diff(x,y)).
>
> I think this is easy enough to achieve without dedicated syntax:
>
> import std.typecons;
>
> template apply(F...){
>     auto apply(T)(T arg){
>         import std.conv, std.range, std.algorithm;
>         return 
> mixin(text("(",iota(F.length).map!(i=>text("F[",i,"](arg)")).join(","),")"));
>     }
> }
>
> void main(){
>     int sum(int a, int b) { return a+b; }
>     int diff(int a, int b) { return a-b; }
>     auto (u,v)=(1,2).apply!(sum, diff);
>     import std.stdio;
>     writeln(u," ",v);
> }

I didn't think of that. That's cool! :)


More information about the Digitalmars-d mailing list