Thread overview
Lambda returning a lambda?
Feb 01, 2018
aliak
Feb 01, 2018
Alex
Feb 01, 2018
Simen Kjærås
Feb 01, 2018
aliak
Feb 02, 2018
Seb
Feb 02, 2018
aliak
February 01, 2018
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
February 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
>
> 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;
}
February 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
February 01, 2018
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) ?


February 02, 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:
>> --
>>   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
February 02, 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#partial

Si si :)

And now I'm thinking, practically, that might be enough. So thanks for the prod.