Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* The Y-Combinator
*/
const y = fn => (f => f(f))(f => fn(x => f(f)(x)));
const factorial = y(fac => n => {
return n < 2 ? 1 : n * fac(n - 1);
});
const fibonacci = y(fib => n => {
return n <= 2 ? 1 : fib(n - 1) + fib(n - 2);
});
console.log(factorial(5));
console.log(fibonacci(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment