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 / programmatic.js
Created May 26, 2013 19:29
js: programmatic
//fixed point example
var programmatic = {
tolerance: 0.0001,
isCloseEnough: function(x, y) {
return Math.abs((x - y) / x) / x < this.tolerance;
},
fixedPoint: function(f) {
return function(firstGuess) {
var iterate = function(guess) {
var next = f(guess);
@jremmen
jremmen / naivefib.js
Created May 26, 2013 19:35
js: naive fib
// naive fibs with memoization
function fib(n, acc) {
if(n >= 1) {
if(acc[n]) {
return acc[n];
} else {
acc[n] = fib(n - 1, acc) + fib(n - 2, acc);
return acc[n];
}
@jremmen
jremmen / strnumstrnum.js
Created May 26, 2013 20:04
js: alpha numeric string pattern test
// recursive function to test if string follows a pattern
// of repeating single alpha > numeric or numeric > alpha characters
function strnum(s) {
function iter(t, i) {
if(i > s.length - 1) return true;
else {
if(t === 1) {
if(!isNaN(s[i])) return false;
else return iter(2, i += 1);
@jremmen
jremmen / JuicyTwig.tmTheme
Last active December 18, 2015 03:29
juicy sublime text 2 theme with improved twig lighting pulled twig specific portion of php-twig package extra themes, merged with the juicy theme from daye rees, and then tweeked a few things(not by much)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<!--
======================================================================
Juicy Twig
======================================================================
A Sublime Text 2 / Textmate theme.
Copyright (c) 2012 Dayle Rees.
Released under the MIT License <http://opensource.org/licenses/MIT>
======================================================================
@jremmen
jremmen / recnumeric.js
Last active December 18, 2015 05:39
js: recursive sum, avg, gcd, and lcm
function sum(n) {
return n.length ? n[0] + sum(n.slice(1)) : 0;
}
function avg(n) {
return sum(n) / n.length;
}
function gcd(n1, n2) {
return n1 % n2 === 0 ? n2 : gcd(n2 % n1, n1);
@jremmen
jremmen / enumObj.js
Created June 8, 2013 21:41
js: recursive object enumerator
function enumObj(obj) {
function recur(obj, acc) {
for(var prop in obj) {
if(obj[prop] instanceof Object && typeof obj[prop] === 'object') {
acc.push(recur(obj[prop], []));
} else {
acc.push(prop + ':' + obj[prop]);
}
}
return acc;
@jremmen
jremmen / binarytreeobj.js
Created June 12, 2013 20:55
js: binary tree objects
var Node = function(o){
this.key = o.key;
this.data = o.data;
this.left = {};
this.right = {};
}
var BinaryTree = function(o) {
this.tree = o.tree;
@jremmen
jremmen / knapsacktable.js
Created June 20, 2013 06:34
js:knapsack object, returns optimal solution in total value, total weight, items selected, and a binary selection process
var Knapsack = function(o) {
this.values = o.values;
this.weights = o.weights;
this.capacity = o.capacity;
this.createSolutionTable = function () {
this.table = this.initializeMatrix();
for(i = 1; i <= this.values.length; i++) {
for(k = 0; k < this.capacity; k++) {
@jremmen
jremmen / primes.js
Created June 22, 2013 22:06
js: returns a list of n primes
function primes(n) {
function iter(i, acc) {
if(acc.length > n) return acc;
else if(i > 1 && divisor(2)) {
acc.push(i);
return iter(i + 1, acc);
}
else return iter(i + 1, acc);
function divisor(x) {
if(x > Math.sqrt(i)) return true;
@jremmen
jremmen / optimisticvalue.js
Last active December 18, 2015 21:19
js: optimistic value using linear relaxtion
function generateSortedWorth() {
return options.values.map(function(x, i) {
return {
index: i,
worth: x / options.weights[i],
};
}).sort(function(a, b) {
return a.worth > b.worth ? -1 : 1;
});
}