Skip to content

Instantly share code, notes, and snippets.

View InPermutation's full-sized avatar

Jacob Krall InPermutation

View GitHub Profile
@InPermutation
InPermutation / gist:1257730
Created October 2, 2011 18:24 — forked from jcbozonier/gist:1257652
Aggregating the scores by lol and associating with the name of the user who created it.
var aggregate_scores_by_lol = function(votes, lols, users){
var vote_count = lols.reduce(function(p,lol,i,r){ return p[lol.id] = 0, p; }, {});
var lol_dictionary = lols.reduce(function(p,lol,i,r){ return p[lol.id] = lol, p}, {});
votes.forEach(function(vote){ vote_count[vote.lol_id]++; });
var vote_count_keys = vote_count.map(function(el,ix){ return ix;});
var sorted_lol_ids = vote_count_keys.sort(function(a, b){
return vote_count[b] - vote_count[a];
function mysqlConfigFromEnvironment (){
var dbUrl = process.env.DATABASE_URL;
var parsed = url.parse(dbUrl);
return {
host: parsed.host,
port: parsed.port || 3306,
user: parsed.auth.split(':')[0],
password: parsed.auth.split(":")[1]
};
};
@InPermutation
InPermutation / gist:4695454
Created February 2, 2013 01:19
Node.js: Import everything from a module into the global scope
var p = require('./program');
for(var dep in p) {
global[dep] = p[dep];
}
@InPermutation
InPermutation / recurse.js
Created March 5, 2013 01:40
An example of tail recursion that is not optimized by Node.
var r = recurse(process.argv[2] || 5000, 0);
console.log('found', r);
function recurse(n, j) {
if(n>0) return recurse(n-1, j+1);
else return j;
}
@InPermutation
InPermutation / bisect.js
Last active December 14, 2015 12:38
Usage: node bisect.js <testfile>.js <testfile>.js should expect one integer parameter. bisect.js will binary search and find the integer at which <testfile>.js no longer functions. Useful for figuring out exactly how many stack frames are allowable.
var fork = require('child_process').spawn;
var cProcess = 0;
bounds(0, 1000);
function exec(param){
cProcess++;
return fork(process.argv[0], [process.argv[2], param]);
}
function bounds(min, max) {
@InPermutation
InPermutation / throw.js
Last active December 14, 2015 12:38
throwup(fxn) -> creates the inductive case of exception-tail-call-optimization makecallable(fxn) -> creates the trampoline for exception-tail-call-optimization
function throwup(fxn) {
return function() {
throw [fxn, Array.prototype.slice.call(arguments)];
}
}
function makecallable(fxn) {
return function() {
var params = Array.prototype.slice.call(arguments);
while(params) {
@InPermutation
InPermutation / tco.js
Last active December 14, 2015 12:38
Using throw.js, show how to tail-call optimize a recursive function using ...exceptions?!
var throwup = require('throw').throwup, makecallable = require('throw').makecallable;
var recurse = throwup(_recurse);
var callable_recurse = makecallable(_recurse);
var result = callable_recurse(parseInt(process.argv[2], 10) || 5000, 0);
console.log('found', result);
function _recurse(n, j) {
if(n>0) return recurse(n-1, j+1);
else return j;
@InPermutation
InPermutation / evenness.js
Last active December 14, 2015 13:48
Show how to optimize corecursive functions using …exceptions?!
var throwup = require('throw').throwup, makecallable = require('throw').makecallable;
var even = throwup(_even),
odd = throwup(_odd);
var r = makecallable(_even)(parseInt(process.argv[2], 10) || 5000, 0);
console.log('found2', r);
function _even(n,j) {
if(n>0) return odd(n-1, j+1);
- return HttpResponse(simplejson.dumps(data), mimetype="application/json")
+
+ # The Django Session middleware helpfully adds 'Cookie' to the Vary header if request.session.accessed is true
+ # Firefox won't cache JSON if the response varies by cookie.
+ # Since we mark Cache-Control: private and max-age:60, the attack vector here is very very small:
+ # People on the same machine as the victim, who read the cache within 60 seconds
+ # So, let's fake out the session middleware to not send the Vary: Cookie header, just Vary: Accept-Encoding.
+ request.session.accessed = False
+
+ response = HttpResponse(simplejson.dumps(data), mimetype="application/json; charset=utf-8")
@InPermutation
InPermutation / negzero.js
Created March 29, 2013 20:45
Negative zero is not actually identical to positive zero.
// [true, true, true]:
[
0 === -0,
Math.atan2(0, -0) === Math.PI,
Math.atan2(0, 0) === 0
]
// [false, false]:
[
-0 < 0,