Last active
March 19, 2018 20:54
-
-
Save jameslaneconkling/3098088868f5e6174c923297bc514679 to your computer and use it in GitHub Desktop.
An implementation of the Y Combinator in Javascript
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 = (fn) => ( | |
(_fn) => fn((arg) => (_fn(_fn))(arg)) | |
)( | |
(__fn) => fn((arg) => (__fn(__fn))(arg)) | |
); | |
var factorial = (fact) => (n) => n === 0 ? 1 : n * fact(n - 1); | |
Y(factorial)(7); | |
//> 5040 | |
/* | |
* const _factorial = Y(factorial) | |
* | |
* _factorial = (n) => n === 0 ? 1 : n * _fact(n - 1); | |
* where _fact is (arg) => (_fn(_fn))(arg) | |
* and where _fn is (__fn) => factorial((arg) => (__fn(__fn))(arg)) | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment