Skip to content

Instantly share code, notes, and snippets.

@NobukazuHanada
Created May 10, 2014 09:36
Show Gist options
  • Save NobukazuHanada/3ff7226bbfa9aa8d30f7 to your computer and use it in GitHub Desktop.
Save NobukazuHanada/3ff7226bbfa9aa8d30f7 to your computer and use it in GitHub Desktop.
(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