-
-
Save nicolo-ribaudo/b8dc59878e48cd6123cf to your computer and use it in GitHub Desktop.
JS Y-combinator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Y = a=>(a=>a(a))(b=>a((...a)=>b(b)(...a))); | |
// Usage | |
/* | |
* var result = Y((the function itself) => (...parameters) => { | |
* return result; | |
* })(...parameters); | |
* | |
*/ | |
// Example | |
const gcd = Y(gcd => (n1, n2, ...ns) => { | |
if (n2) return gcd(n2, n1 % n2, ...ns); | |
return n1; | |
}); | |
const lcm = Y(lcm => (n1, n2, ...ns) => { | |
if (n2) lcm(n1 * n2 / gcd(n1, n2), ...ns); | |
return n1; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment