diff options
Diffstat (limited to 'src/third_party/js-1.7/Y.js')
| -rw-r--r-- | src/third_party/js-1.7/Y.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/third_party/js-1.7/Y.js b/src/third_party/js-1.7/Y.js new file mode 100644 index 00000000000..e92a65a5df9 --- /dev/null +++ b/src/third_party/js-1.7/Y.js @@ -0,0 +1,19 @@ +// The Y combinator, applied to the factorial function + +function factorial(proc) { + return function (n) { + return (n <= 1) ? 1 : n * proc(n-1); + } +} + +function Y(outer) { + function inner(proc) { + function apply(arg) { + return proc(proc)(arg); + } + return outer(apply); + } + return inner(inner); +} + +print("5! is " + Y(factorial)(5)); |
