Skip to content

Instantly share code, notes, and snippets.

View jremmen's full-sized avatar

John Remmen jremmen

  • Planning Center
  • San Diego
View GitHub Profile
@jremmen
jremmen / featurescale.js
Last active August 29, 2015 13:57
js: feature scaling using max - min or standard deviation
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]
@jremmen
jremmen / isequal.js
Last active August 29, 2015 14:01
js: object/array isEqual method
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;
@jremmen
jremmen / insert_sort.js
Created May 30, 2014 05:31
js: insert sort
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)));
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 {
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,
@jremmen
jremmen / multimap_multireduce.js
Last active August 29, 2015 14:02
js: multimap and multireduce array methods
Array.prototype.multimap = function(f) {
return this.length == 0 ? [] : [(this[0] instanceof Array ? this[0].multimap(f) : f(this[0]))].concat(this.slice(1).multimap(f));
}
Array.prototype.multireduce = function(f, z) {
return this.length == 0 ? z : f((this[0] instanceof Array ? this[0].multireduce(f, z) : this[0]), this.slice(1).multireduce(f, z));
}
@jremmen
jremmen / jquery.tripleclick.js
Last active August 29, 2015 14:02
jquery: tripleclick event
(function($) {
$.event.special.tripleclick = {
clicked: 0,
time: 0,
threshold: 80,
setup: function() {
$(this).on('click', $.event.special.tripleclick.handler);
@jremmen
jremmen / jquery.zoomblo.js
Last active August 29, 2015 14:02
jquery: zoomblo, light weight image zoomer
(function($) {
$.fn.zoomblo = function(full) {
$(this).css({
position: 'relative',
overflow: 'hidden'
});
var org = {
@jremmen
jremmen / submax.js
Created July 2, 2014 22:36
js: maximum sub array solution using divide and conquer
// O(n log n)
Array.prototype.submax = function() {
if(this.length === 0) return 0;
if(this.length === 1) return this[0];
var m = Math.floor(this.length / 2);
var max_left = find_max(this, m - 1, 0);
var max_right = find_max(this, m, this.length);
@jremmen
jremmen / submax.js
Created July 2, 2014 23:14
js: maximum sub array in linear time
// O(n)
Array.prototype.submax = function() {
var max = sum = 0;
for(var i = 0, l = this.length; i < l; i++) {
sum = Math.max(sum + this[i], 0);
max = Math.max(max, sum);
}
return max;
}