Last active
August 29, 2015 14:04
-
-
Save wchaowu/c1698359650b18736c68 to your computer and use it in GitHub Desktop.
函数式编程 lambda表达
This file contains hidden or 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
| 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