Skip to content

Instantly share code, notes, and snippets.

@jameslaneconkling
Last active March 19, 2018 20:54
Show Gist options
  • Save jameslaneconkling/3098088868f5e6174c923297bc514679 to your computer and use it in GitHub Desktop.
Save jameslaneconkling/3098088868f5e6174c923297bc514679 to your computer and use it in GitHub Desktop.
An implementation of the Y Combinator in Javascript
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