This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var List = function() { | |
return (function build(xs) { | |
if(xs.length === 0) return Nil | |
return new Cons(xs[0], build(Array.prototype.slice.call(xs, 1))); | |
})(arguments); | |
} | |
var Nil = { | |
head: null, | |
tail: null, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Empty = { | |
contains: function(x) { return false; }, | |
incl: function(x) { return new NonEmpty(x, Empty, Empty); }, | |
union: function(other) { return other; }, | |
toString: function() { return '.'; } | |
}; | |
var NonEmpty = function(elem, left, right) { | |
return { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorting = { | |
insert_sort: function(xs) { | |
function insert(x, xs) { | |
if(xs.length === 0) return [x]; | |
else return x <= xs[0] ? [x].concat(xs) : [xs[0]].concat(insert(x, xs.slice(1))); | |
} | |
return xs.length === 0 ? [] : insert(xs[0], this.insert_sort(xs.slice(1))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.prototype.isEqual = function(obj) { | |
function iter(a, b) { | |
for(p in a) { | |
if(a.hasOwnProperty(p)) { | |
if(!b.hasOwnProperty(p)) return false; | |
else if(['array', 'object'].indexOf(typeof b[p]) > -1) { | |
if(!iter(b[p], a[p])) return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scale = function(a,d) { return a.map(function(x) { return (x - avg(a)) / d}) } | |
avg = function(a) { return sum(a) / a.length } | |
sum = function(a) { return a.reduce(function(x,y) { return x + y; }) } | |
max = function(a) { return a.sort(function(x,y) { return x < y; })[0] } | |
min = function(a) { return a.sort(function(x,y) { return x > y; })[0] } | |
sd = function(a,av) { return Math.sqrt(avg(a.map(function(x) { return (x - av) * x; }))); } | |
z = [20,25,10,33,50,42,19] | |
scale(z,(max(z) - min(z))) // [-0.2107142857142857, -0.23571428571428568, -0.2107142857142857, -0.08571428571428567, 0.11428571428571432, 0.3392857142857143, 0.5392857142857144] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zipw = function(a,b,f) { | |
var c = a.length < b.length ? {a:a, b:b} : {a:b, b:a}; | |
return c.a.map(function(x,i) { | |
return f(x,c.b[i]); | |
}); | |
} | |
zipw([1,2,3],[10,11,12], function(x,y) { return x + y; }); // [11,13,15] | |
zipw([1,2,3],[10,11,12], function(x,y) { return x * y; }); // [10,22,36] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mmultiply = function(a,b) { | |
return a.map(function(x,i) { | |
return transpose(b).map(function(y,k) { | |
return dotproduct(x, y) | |
}); | |
}); | |
} | |
dotproduct = function(a,b) { | |
return a.map(function(x,i) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function im(n) { var a = Array.apply(null, new Array(n)); return a.map(function(x, i) { return a.map(function(y, k) { return i === k ? 1 : 0; }) }) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function text2bin(msg) { return msg.split('').map(function(x) { var b = x.charCodeAt(0).toString(2); return Array(9 - b.length).join(0) + b; }).join(''); } | |
function bin2text(bin) { return bin.match(/(.{1,8})/g).map(function(x) { return String.fromCharCode(parseInt(x, 2)) }).join(''); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calculate = function(a, f) { return a.reduce(function(x,y) { return f(x, y) }); } | |
sum = function(a, b) { return a + b; } | |
sub = function(a, b) { return a - b; } | |
sumAll = function(a) { return calculate(a, sum); } | |
subAll = function(a) { return calculate(a, sub); } |