www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Lambda returning a lambda?

reply aliak <something something.com> writes:
Is there a way to do this:

import std.stdio;

void main()
{
     alias f = (a) => (b) => a * b;
     f(2)(3).writeln;
}

Error now is: Error: template lambda has no type

Cheers
Feb 01 2018
next sibling parent Alex <sascha.orlov gmail.com> writes:
On Thursday, 1 February 2018 at 11:51:11 UTC, aliak wrote:
 Is there a way to do this:

 import std.stdio;

 void main()
 {
     alias f = (a) => (b) => a * b;
     f(2)(3).writeln;
 }

 Error now is: Error: template lambda has no type

 Cheers
This works: void main() { auto f = (size_t x) => (size_t y) => x * y; f(2)(3).writeln; } and this: void main() { alias f(T) = (T x) => (T y) => x * y; f!size_t(2)(3).writeln; }
Feb 01 2018
prev sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Thursday, 1 February 2018 at 11:51:11 UTC, aliak wrote:
 Is there a way to do this:

 import std.stdio;

 void main()
 {
     alias f = (a) => (b) => a * b;
     f(2)(3).writeln;
 }
The problem here is the outer lambda doesn't know what type the inner lambda is. We can rewrite this a bit: auto f(T)(T a) { return b => a * b; } What type should this function return? Sure, it's possible to define such a type: auto f(T)(T a) { struct Result { auto opCall(T2)(T2 b) { return a * b; } } Result r; return r; } However, that type is not a lambda, but a specialized type that only does the right thing in some very special cases. -- Simen
Feb 01 2018
parent reply aliak <something something.com> writes:
On Thursday, 1 February 2018 at 14:28:34 UTC, Simen Kjærås wrote:
 --
   Simen
Ah, thank you both. For solution and explanation. Me wonders... are there any thoughts around having functions return aliases as well? I have no idea if it's even possible but if it is, then does the initial syntax become possible? Or some kind of lazy type deduction that just tries to determine type when actually needed, and everything is an alias until that point? Would a library curry solution on an alias lambda be possible? alias f = curry!((a, b) => a * b) ?
Feb 01 2018
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 1 February 2018 at 15:41:00 UTC, aliak wrote:
 On Thursday, 1 February 2018 at 14:28:34 UTC, Simen Kjærås 
 wrote:
 --
   Simen
Ah, thank you both. For solution and explanation. Me wonders... are there any thoughts around having functions return aliases as well? I have no idea if it's even possible but if it is, then does the initial syntax become possible? Or some kind of lazy type deduction that just tries to determine type when actually needed, and everything is an alias until that point? Would a library curry solution on an alias lambda be possible? alias f = curry!((a, b) => a * b) ?
Are you aware of partial? https://dlang.org/phobos/std_functional.html#partial
Feb 01 2018
parent aliak <something something.com> writes:
On Friday, 2 February 2018 at 01:31:15 UTC, Seb wrote:
 Are you aware of partial?

 https://dlang.org/phobos/std_functional.html#partial
Si si :) And now I'm thinking, practically, that might be enough. So thanks for the prod.
Feb 02 2018