Self-recursion and arrow functions

Jorge Chamorro jorge at
Sun Mar 17 16:10:39 PDT 2013

On 17/03/2013, at 14:33, Mark S. Miller wrote:

> Just in case anyone does not realize that this thread is humorous, 
>     const factorial = n => n>1 ? n*factorial(n-1) : 1;
> Yes, you can't use this as an expression. So what? After this declaration you can use factorial as an expression.

IIRC the possibility of *simply* using 'ƒ' (instead of 'function') for lambdas, which is a syntax that's immediately familiar to any JS developer:

[1,2,3,4,5,6].map(ƒ factorial(n) { n>1 ? n*factorial(n-1) : 1 });

and could also give us anonymous and named lambdas that can be used à la 'function' function:

A lambda declaration: ƒ factorial(n) { n>1 ? n*factorial(n-1) : 1 }

A named lambda expression: (ƒ factorial(n) n>1 ? n*factorial(n-1) : 1)(5);

Anonymous lambdas:

[1,2,3,4,5,6].map(ƒ(n) n*2);  //ƒ lambdas are even shorter than arrow functions
[1,2,3,4,5,6].map((n)=> n*2);

was being considered in the past, but we ended up with arrows instead. I for one liked ƒs much more than arrows because ƒ()... looks like a function much more than the grawlixy ()=>...

Now, given that ƒs could be created named or anonymous and used as expressions in any case, with ease, just like the regular functions can, perhaps ƒ-lambdas may deserve a reconsideration?

> Anonymous lambda expressions are a wonderful things. But in actual development, I have never yet encountered a recursive function I didn't want to name.

But Brandon Benvie was pointing out at the problem: "Relying on the defined name they're assigned to suffers from the "can be redefined" problem":

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

The factorial lambda above depends on a free var to function properly which is a hazard.

It never ocurred to me that using const instead of var/let as you've done above fixes that, thank you!

Still, ƒ named lambdas have the advantage that can be used directly as expressions, without going through any const roundabouts.

> Ok, and now back to our irregularly scheduled humor...


More information about the es-discuss mailing list