Last active
August 16, 2017 19:12
-
-
Save RobinMalfait/ba1eb7392fba79f2887cd80a41db6b77 to your computer and use it in GitHub Desktop.
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
/** | |
* 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