Created
May 10, 2014 09:36
-
-
Save NobukazuHanada/3ff7226bbfa9aa8d30f7 to your computer and use it in GitHub Desktop.
This file contains 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
(function () | |
{ | |
function partial(fun) | |
{ | |
var _slice = [].slice, | |
args = _slice.call(arguments); | |
args.shift(); | |
return function () | |
{ | |
return fun.apply(this, args.concat(_slice.call(arguments))); | |
}; | |
} | |
var expr = function (n) | |
{ | |
var utilities = { | |
"+": function (a, b) { return a + b }, | |
"-": function (a, b) { return a - b }, | |
"*": function (a, b) { return a * b }, | |
"/": function (a, b) { return a / b }}; | |
var _n = n, | |
_f = function () {}; | |
return function outer(op) | |
{ | |
if (op === "=") return _n; | |
_f = partial(utilities[op], _n); | |
return function (m) | |
{ | |
_n = _f(m); | |
return outer; | |
} | |
}; | |
}; | |
console.log( expr(4)("+")(3)("*")(2)("-")(1)("=") ); // 13 | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment