Skip to content

Commit

Permalink
a bunch of infix operators
Browse files Browse the repository at this point in the history
  • Loading branch information
farskipper committed Sep 25, 2017
1 parent 774ae9f commit a35650d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/node-krl-stdlib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,60 @@ var _ = require("lodash");

var stdlib = {};

var defVarArgOp = function(op, reducer){
stdlib[op] = function(){
if(arguments.length === 0){
return;
}
var r = arguments[0];
var i;
for(i = 1; i < arguments.length; i++){
r = reducer(r, arguments[i]);
}
return r;
};
};

defVarArgOp("||", function(r, a){
return r || a;
});
defVarArgOp("&&", function(r, a){
return r && a;
});
defVarArgOp("<", function(r, a){
return r < a;
});
defVarArgOp(">", function(r, a){
return r > a;
});
defVarArgOp("<=", function(r, a){
return r <= a;
});
defVarArgOp(">=", function(r, a){
return r >= a;
});
defVarArgOp("==", function(r, a){
return r === a;
});
defVarArgOp("!=", function(r, a){
return r !== a;
});
defVarArgOp("+", function(r, a){
return r + a;
});
defVarArgOp("-", function(r, a){
return r - a;
});
defVarArgOp("*", function(r, a){
return r * a;
});
defVarArgOp("/", function(r, a){
return r / a;
});
defVarArgOp("%", function(r, a){
return r % a;
});

stdlib["+"] = function(){
if(arguments.length === 0){
return;
Expand Down
23 changes: 23 additions & 0 deletions packages/node-krl-stdlib/src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@ var testFn = function(t, fn, args, expected, message){
t.deepEquals(stdlib[fn].apply(null, args), expected, message);
};

test("general operators", function(t){
var tf = _.partial(testFn, t);
tf("+", [], void 0);
tf("+", [1], 1);
tf("+", [1, 2], 3);
tf("+", [1, 2, 3], 6);
tf("-", [1, 2, 3], -4);

tf("||", [false, false], false);
tf("||", [true, false], true);
tf("&&", [true, false], false);
tf("&&", [true, true], true);

tf("<", [1, 3], true);
tf("<", [3, 1], false);

tf("*", [5, 2], 10);
tf("/", [4, 2], 2);
tf("%", [4, 2], 0);

t.end();
});

test("general operators", function(t){

t.equals(stdlib.as(1, "String"), "1");
Expand Down

0 comments on commit a35650d

Please sign in to comment.