Thread overview
[OT] What do you think about declaring functions with lambda syntax?
Dec 04, 2020
ddcovery
Dec 04, 2020
ddcovery
Dec 05, 2020
IGotD-
Dec 05, 2020
ddcovery
Dec 05, 2020
user1234
Dec 05, 2020
ddcovery
December 04, 2020
Example:

import
  std.stdio,
  std.algorithm,
  std.array,
  std.conv
;

ulong factorial(ulong n) =>
  n>1 ? factorial(n-1)*n : 1
;

T[] sorted(T)(T[] xs) =>
  xs.length == 0 ? [] :
    xs[1..$].filter!(x=> x < xs[0]).array.sorted ~
    xs[0..1] ~
    xs[1..$].filter!(x=> x >= xs[0]).array.sorted
;

void main() =>
  [5,6,7,2,3].sorted.map!(i=>i.to!ulong.factorial).writeln
;

December 04, 2020
On Friday, 4 December 2020 at 11:12:23 UTC, ddcovery wrote:
> Example:
> ...

This is more functional :-)

import
  std.stdio,
  std.algorithm,
  std.array,
  std.conv
;

pure ulong factorial(ulong n) =>
  n>1 ? n * factorial(n-1) : 1
;

pure T[] sorted(T)(T[] xs) =>
  xs.length == 0 ? [] :
    xs[1..$].filter!(x=> x < xs[0]).array.sorted ~
    xs[0..1] ~
    xs[1..$].filter!(x=> x >= xs[0]).array.sorted
;

void main() =>
  [5,6,7,2,3].sorted.map!(i=>i.to!ulong.factorial).writeln
;
December 05, 2020
On Friday, 4 December 2020 at 11:12:23 UTC, ddcovery wrote:
> Example:
>
> import
>   std.stdio,
>   std.algorithm,
>   std.array,
>   std.conv
> ;
>
> ulong factorial(ulong n) =>
>   n>1 ? factorial(n-1)*n : 1
> ;
>
> T[] sorted(T)(T[] xs) =>
>   xs.length == 0 ? [] :
>     xs[1..$].filter!(x=> x < xs[0]).array.sorted ~
>     xs[0..1] ~
>     xs[1..$].filter!(x=> x >= xs[0]).array.sorted
> ;
>
> void main() =>
>   [5,6,7,2,3].sorted.map!(i=>i.to!ulong.factorial).writeln
> ;

It is really ugly and doesn't help readability. It looks like another language.
December 05, 2020
On Friday, 4 December 2020 at 11:12:23 UTC, ddcovery wrote:
> Example:
There's a related proposal [1] and the NG discussion is there [2].

[1] https://github.com/dlang/dmd/pull/11833
[2] https://forum.dlang.org/post/cmgqwabzdqbtngmjidfw@forum.dlang.org
December 05, 2020
On Saturday, 5 December 2020 at 00:55:23 UTC, IGotD- wrote:
>
> It is really ugly and doesn't help readability. It looks like another language.

For me it is absolutely wonderful and totally readable (expressive)...  this is the way I like to write code when possible and this is, basically, D.

Yesterday, I found myself writing this:

ulong factorial(ulong n){return
  n>1 ? n*factorial(n-1) : 1
;}

Lambda notation is "syntax sugar" that compiler can transform to  "{return ... }" transparently

You choose your favorite one for each situation (like f(x) or x.f() or x.f,  or like named parameters that will be introduced in future versions of D)

Dart already offers this capability.

// named function c like notation
int factorial(int n) { return ...; }
// named function lambda notation
int factorial(int n) => ...;
// Anonymous function c like notation
final int Function(int) f = (n){ return n*n; }
// Anonymous function lambda notation
final int Function(int n) f = (n) => n*n;

Scala does something similar (not really lambda notation, only the possibility to remove brackets because "return" is implicit)

def factorial(n:Int):Int = if (n>1) n * factorial(n-1) else 1;
def factorial(n:Int):Int = { if (n>1) n * factorial(n-1) else 1; }

In typescript and python you can assign a lambda to a variable and refer it from the body
// Typescript
const factorial = (n:bigint)=> n>1? n*factorial(n-1):1;
// Python
factorial = lambda n : factorial(n-1)*n if n>1 else 1;

This is not the est solution, but this is something (simple and effective)

As DConf2020 exposed  "D, the functional programming language nobody is talking about".  In my opinion, the possibility of functions without brackets/return would be a good reinforcement.


December 05, 2020
On Saturday, 5 December 2020 at 03:06:02 UTC, user1234 wrote:
> On Friday, 4 December 2020 at 11:12:23 UTC, ddcovery wrote:
>> Example:
> There's a related proposal [1] and the NG discussion is there [2].
>
> [1] https://github.com/dlang/dmd/pull/11833

I love it!!!

Thanks