Skip to content

Instantly share code, notes, and snippets.

@digitalicarus
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalicarus/f37693972ee71c75b197 to your computer and use it in GitHub Desktop.
Save digitalicarus/f37693972ee71c75b197 to your computer and use it in GitHub Desktop.
Reactive Expressions
var EX = function (expr) {
var result
, operands = {}
, operVals = Array.prototype.slice.call(arguments, 1)
, operSetFuncs = {}
;
function genResult () {
result = expr.apply(null, Object.keys(operands).map(function(v) {
return operands[v];
}));
}
expr.toString().replace(/^[^(]*\(/g, '').replace(/\).*/g, '').split(/\s*,\s*/).forEach(function(v, i) {
var curr = v;
operands[curr] = operVals[i];
operSetFuncs[curr] = function (val) {
if (val !== undefined) {
operands[curr] = val;
genResult();
}
return operands[curr];
}
});
genResult();
return [
function () {
return result;
}, operSetFuncs
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment