How Do I Write An Arrow Function In Es6 Recursively?
Solution 1:
Writing a recursive function without naming it is a problem that is as old as computer science itself (even older, actually, since λ-calculus predates computer science), since in λ-calculus all functions are anonymous, and yet you still need recursion.
The solution is to use a fixpoint combinator, usually the Y combinator. This looks something like this:
(y =>y(
givenFact =>n =>
n < 2 ? 1 : n * givenFact(n-1)
)(5)
)(le =>
(f =>f(f)
)(f =>le(x => (f(f))(x))
)
);
This will compute the factorial of 5
recursively.
Note: the code is heavily based on this: The Y Combinator explained with JavaScript. All credit should go to the original author. I mostly just "harmonized" (is that what you call refactoring old code with new features from ES/Harmony?) it.
Solution 2:
It looks like you can assign arrow functions to a variable and use it to call the function recursively.
varcomplex = (a, b) => {
if (a > b) {
return a;
} else {
complex(a, b);
}
};
Solution 3:
Claus Reinke has given an answer to your question in a discussion on the esdiscuss.org website.
In ES6 you have to define what he calls a recursion combinator.
letrec = (f)=> (..args)=>f( (..args)=>rec(f)(..args), ..args )
If you want to call a recursive arrow function, you have to call the recursion combinator with the arrow function as parameter, the first parameter of the arrow function is a recursive function and the rest are the parameters. The name of the recursive function has no importance as it would not be used outside the recursive combinator. You can then call the anonymous arrow function. Here we compute the factorial of 6.
rec( (f,n) => (n>1 ? n*f(n-1) : n) )(6)
If you want to test it in Firefox you need to use the ES5 translation of the recursion combinator:
functionrec(f){
returnfunction(){
return f.apply(this,[
function(){
returnrec(f).apply(this,arguments);
}
].concat(Array.prototype.slice.call(arguments))
);
}
}
Solution 4:
Use a variable to which you assign the function, e.g.
constfac = (n) => n>0 ? n*fac(n-1) : 1;
If you really need it anonymous, use the Y combinator, like this:
constY = (f) => ((x)=>f((v)=>x(x)(v)))((x)=>f((v)=>x(x)(v)))
… Y((fac)=>(n)=> n>0 ? n*fac(n-1) : 1) …
Solution 5:
TL;DR:
constrec = f => f((...xs) =>rec(f)(...xs));
There are many answers here with variations on a proper Y -- but that's a bit redundant... The thing is that the usual way Y is explained is "what if there is no recursion", so Y itself cannot refer to itself. But since the goal here is a practical combinator, there's no reason to do that. There's this answer that defines rec
using itself, but it's complicated and kind of ugly since it adds an argument instead of currying.
The simple recursively-defined Y is
const rec = f => f(rec(f));
but since JS isn't lazy, the above adds the necessary wrapping.
Post a Comment for "How Do I Write An Arrow Function In Es6 Recursively?"