Skip to content

Instantly share code, notes, and snippets.

@RobinMalfait
Last active August 16, 2017 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobinMalfait/ba1eb7392fba79f2887cd80a41db6b77 to your computer and use it in GitHub Desktop.
Save RobinMalfait/ba1eb7392fba79f2887cd80a41db6b77 to your computer and use it in GitHub Desktop.
/**
* 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