This file contains 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
//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); |
This file contains 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
// 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]; | |
} |
This file contains 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
// 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); |
This file contains 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
<?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> | |
====================================================================== |
This file contains 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 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); |
This file contains 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 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; |
This file contains 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 Node = function(o){ | |
this.key = o.key; | |
this.data = o.data; | |
this.left = {}; | |
this.right = {}; | |
} | |
var BinaryTree = function(o) { | |
this.tree = o.tree; |
This file contains 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 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++) { |
This file contains 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 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; |
This file contains 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 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; | |
}); | |
} |
OlderNewer