digitalmars.D.learn - Lambda returning a lambda?
- aliak (9/9) Feb 01 2018 Is there a way to do this:
- Alex (13/22) Feb 01 2018 This works:
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (21/28) Feb 01 2018 The problem here is the outer lambda doesn't know what type the
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
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 CheersThis 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
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
On Thursday, 1 February 2018 at 14:28:34 UTC, Simen Kjærås wrote:-- SimenAh, 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
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:Are you aware of partial? https://dlang.org/phobos/std_functional.html#partial-- SimenAh, 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
On Friday, 2 February 2018 at 01:31:15 UTC, Seb wrote:Are you aware of partial? https://dlang.org/phobos/std_functional.html#partialSi si :) And now I'm thinking, practically, that might be enough. So thanks for the prod.
Feb 02 2018