Permalink
Browse files

expanded built in functions, more higher order functions

  • Loading branch information...
1 parent dc0a114 commit 32a253ff1f8a4cf6019ee2a3a773655a209d1fb2 @benbscholz committed Apr 27, 2012
Showing with 49 additions and 4 deletions.
  1. +49 −4 src/built_ins.js
View
@@ -7,14 +7,18 @@
*/
var add_proc = function () {
var i,
+ val;
+ if (typeof arguments[0] === "string")
+ val = '';
+ else
val = 0;
for (i = 0; i < arguments.length; i += 1) {
val += arguments[i];
}
return val;
};
-var mult_proc = function () {
+var mlt_proc = function () {
var i,
val = 1;
for (i = 0; i < arguments.length; i += 1) {
@@ -41,6 +45,16 @@ var div_proc = function () {
return val;
};
+var and = function () {
+ var args = [],
+ i,
+ bool = arguments[0];
+ for (i = 1; i < arguments.length; i += 1) {
+ bool = bool && arguments[i];
+ }
+ return bool;
+};
+
var list = function () {
var args = [],
i;
@@ -57,6 +71,18 @@ var vector_set = function (v, p, val) {
return a;
};
+var vector_ref = function (v, p) {
+ return v[p];
+};
+
+var build_list = function (n) {
+ var i, l = [];
+ for (i = 0; i < n; i += 1) {
+ l.push(i);
+ }
+ return l;
+};
+
var _isArray = function (item) {
if (item && typeof item === 'object' && item.constructor === Array)
return true;
@@ -66,14 +92,30 @@ var _isArray = function (item) {
return false;
};
+var append = function (l, v) {
+ if (_isArray(l)) {
+ l.push(v);
+ return l;
+ } else {
+ return [l,v];
+ }
+};
+
var mod_proc = function (a,b) {return a%b;};
var lt_proc = function (a,b) {return a<b;};
var gt_proc = function (a,b) {return a>b;};
var lte_proc = function (a,b) {return a<=b;};
var gte_proc = function (a,b) {return a>=b;};
-var eq_proc = function (a,b) {return a===b;};
+var eql_proc = function (a,b) {return a===b;};
var car = function (a) {return a[0];};
-var cdr = function (a) {return a.slice(1,a.length);};
+var cdr = function (a) {
+ var isa = _isArray(a);
+ if (!isa || a.length === 1) {
+ return [];
+ } else {
+ return a.slice(1,a.length);
+ }
+};
var first = car;
var rest = cdr;
var cons = function (a,b) {
@@ -87,9 +129,11 @@ var cons = function (a,b) {
} else if (isb) {
return b.push(a);
} else {
- return [a,b,[]];
+ return [a,b];
}
};
+var functionOf = function (o,f,a) { return o[f](a);};
+var property = function (a,b) {return a[b];};
var empty_bool = function (a) {return (a instanceof Array && a.length === 0);};
var list_bool = function (a) {return _isArray(a) && a[-1] === [];};
var equal_bool = function (a,b) {return a==b;};
@@ -104,6 +148,7 @@ var vector_ref = function (v, p) {return v[p];};
var vector_append = function (a, b) {return a.concat(b);};
var empty = [];
var not = function (a) { return !a; };
+var blank = function () { return; };
var add1 = function (a) { return a+1; };
var sub1 = function (a) { return a-1; };
var abs = Math.abs;

0 comments on commit 32a253f

Please sign in to comment.