Skip to content

Instantly share code, notes, and snippets.

@wchaowu
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save wchaowu/c1698359650b18736c68 to your computer and use it in GitHub Desktop.

Select an option

Save wchaowu/c1698359650b18736c68 to your computer and use it in GitHub Desktop.
函数式编程 lambda表达
var Y = function (f) {
return (function (g) {
return g(g);
}) (function (h) {
return function () {
return f(h(h)) .apply(null, arguments);
};
});
};
//利用y因子 实现阶乘 n*(n-1)*(n-2) *****1
var factorial = Y(function(func){
return function(x){
return x == 0 ? 1 : x * func(x - 1);
}
});
console.log(factorial(10));
//或者
/*console.log(Y(function (func){
return function (x){
return x == 0 ? 1 : x * func(x - 1);
}
})(10));*/
//或者
/*
console.log((function (x) {
return x == 0 ? 1 : x * arguments.callee(x - 1);
}) (10));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment