mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 14:22:01 +00:00
6b433caaaa
Initial check-in to mozilla tree: JSRef development is migrating from JSFUN13_BRANCH of /m/src repository to /m/pub
22 lines
370 B
JavaScript
22 lines
370 B
JavaScript
// The Y combinator, applied to the factorial function
|
|
|
|
function factwrap(proc) {
|
|
function factproc(n) {
|
|
if (n <= 1) return 1
|
|
return n * proc(n-1)
|
|
}
|
|
return factproc
|
|
}
|
|
|
|
function Y(outer) {
|
|
function inner(proc) {
|
|
function apply(arg) {
|
|
return proc(proc)(arg)
|
|
}
|
|
return outer(apply)
|
|
}
|
|
return inner(inner)
|
|
}
|
|
|
|
print("5! is " + Y(factwrap)(5))
|