Self-recursion and arrow functions

Claus Reinke claus.reinke at talk21.com
Sun Mar 17 02:43:31 PDT 2013


> I understand, but it's still a limitation of arrow functions that they rely
> on arguments.callee to self-reference. Relying on the defined name
> they're assigned to suffers from the "can be redefined" problem. NFE's
> don't suffer this problem and can completely avoid `arguments` in ES6
> for all use cases Arrow functions, currently, cannot.

Neither arguments.callee (not available in strict) nor let (clumsy to
use in expressions) are needed for self-reference

    var rec = (f) => f((...args)=>rec(f)(...args));

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

    [1,2,3,4,5,6].map((n)=>rec(f)(n));

You can try it out in traceur (example link at the bottom)

    http://traceur-compiler.googlecode.com/git/demo/repl.html

Claus

http://traceur-compiler.googlecode.com/git/demo/repl.html#var%20rec%20%3D%20(f)%20%3D%3E%20f((...args)%3D%3Erec(f)(...args))%3B%0A%0Avar%20f%20%3D%20(self)%3D%3E(n)%3D%3E%20n%3E1%20%3F%20n*self(n-1)%20%3A%20n%3B%0A%0A%5B1%2C2%2C3%2C4%2C5%2C6%5D.map((n)%3D%3Erec(f)(n))%3B

 



More information about the es-discuss mailing list