Permalink
Browse files

improved createFn

  • Loading branch information...
1 parent 158f1a1 commit f8e0de8cf16b3952e2b741027104b10998710600 @chrisdew committed Dec 4, 2012
Showing with 18 additions and 14 deletions.
  1. +18 −14 lib/table.js
View
@@ -135,48 +135,52 @@ function createFn(expr) {
}
assert(expr.fn);
- if (expr.args.length === 0) {
- // 0-arity functions
- } else if (expr.args.length === 1) {
- var fn0 = createFn(expr.args[0]);
+ var arity = expr.args.length;
+ var fns = [];
+ for (var i = 0; i < arity; i++) {
+ fns.push(createFn(expr.args[i]));
+ }
+
+ if (arity === 0) {
+ } else if (arity === 1) {
if (expr.fn === 'equalsOne') {
return function(r) {
- return fn0(r) === 1;
+ return fns[0](r) === 1;
}
}
- } else if (expr.args.length === 2) {
- var fn0 = createFn(expr.args[0]);
- var fn1 = createFn(expr.args[1]);
+ } else if (arity === 2) {
if (expr.fn === 'equal') {
return function(r) {
- return fn0(r) === fn1(r);
+ return fns[0](r) === fns[1](r);
}
}
if (expr.fn === 'and') {
return function(r) {
- return fn0(r) && fn1(r);
+ return fns[0](r) && fns[1](r);
}
}
if (expr.fn === 'gt') {
return function(r) {
- return fn0(r) > fn1(r);
+ return fns[0](r) > fns[1](r);
}
}
if (expr.fn === 'gte') {
return function(r) {
- return fn0(r) >= fn1(r);
+ return fns[0](r) >= fns[1](r);
}
}
if (expr.fn === 'lt') {
return function(r) {
- return fn0(r) < fn1(r);
+ return fns[0](r) < fns[1](r);
}
}
if (expr.fn === 'lte') {
return function(r) {
- return fn0(r) <= fn1(r);
+ return fns[0](r) <= fns[1](r);
}
}
+ } else {
+ throw "cannot handle arity of " + arity + " in " + JSON.stringify(expr);
}
throw "cannot handle " + JSON.stringify(expr);
}

0 comments on commit f8e0de8

Please sign in to comment.