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 opop(obj,k) { | |
for(o in obj) { | |
if(o == k) { | |
result = obj[o]; | |
delete obj[o]; | |
return result; | |
} | |
else if(typeof obj[o] == 'object' && obj[o].constructor == Object) { | |
if(a = opop(obj[o], k)) return a; | |
} |
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 mergesort(list) { | |
if(list.length <= 1) return list; | |
var l = mergesort(list.slice(0, list.length / 2)); | |
var r = mergesort(list.slice(list.length / 2)); | |
return merge(l, r); | |
} | |
function merge(l, r) { | |
var result = []; | |
while(l.length > 0 || r.length > 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
var Point = function(x, y) { | |
this.x = x; | |
this.y = y; | |
}; | |
function getDistance(a, b) { | |
return Math.sqrt(Math.pow((a.x - b.x), 2) + Math.pow((a.y - b.y), 2)); | |
}; |
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 quick_sort(a) { | |
var m = a.length; | |
if(m <= 1) return a; | |
var v = a.shift(); | |
var l = []; | |
var r = []; |
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 max = reduce(function(a, b) { return a > b; }); | |
var min = reduce(function(a, b) { return a < b; }); | |
function reduce(p) { | |
return function(a) { | |
function iter(a, c) { | |
if(a.length === 0) return c; | |
else return iter(a.slice(1), p(a[0], c) ? a[0] : c); | |
} | |
return iter(a.slice(1), a[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
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); } |
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
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
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
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] |