Skip to content

Instantly share code, notes, and snippets.

View dgraham's full-sized avatar
💭

David Graham dgraham

💭
View GitHub Profile

Keybase proof

I hereby claim:

  • I am dgraham on github.
  • I am dgraham (https://keybase.io/dgraham) on keybase.
  • I have a public key whose fingerprint is 87B9 DDE7 41E3 D8B8 0DC7 F2E4 AA94 05DC E2E0 D208

To claim this, I am signing this object:

@dgraham
dgraham / delegated.js
Last active August 29, 2015 14:25
Simple delegated event handling.
(function() {
const events = new Map();
const stopped = new WeakMap();
function before(subject, verb, fn) {
const source = subject[verb];
subject[verb] = function() {
fn.apply(subject, arguments);
return source.apply(subject, arguments);
};
@dgraham
dgraham / gist:1260596
Created October 4, 2011 00:10
EM::PriorityQueue using RBTree
module EventMachine
class PriorityQueue < Queue
def initialize(&comparator)
super
@items = Items.new(comparator)
end
class Items
def initialize(comparator)
require 'rbtree'
@dgraham
dgraham / gist:2210713
Created March 26, 2012 23:41
Fibered Recursive Descent JSON Parser
#!/usr/bin/env ruby
require 'fiber'
# A resumable, recursive descent JSON parser, using Fibers.
# http://www.negativecode.com/posts/2012/03/26/fibers-resumable-recursive-descent-json-parsing/
class Parser
Result = Struct.new(:value)
def initialize
@dgraham
dgraham / anchor.js
Last active October 5, 2015 03:15
Simulate browsers' scroll-to-anchor behavior on page load.
(function() {
function hashchange() {
if (!location.hash) {
return;
}
// Don't do anything if the current target exists.
if (document.querySelector(":target")) {
return;
}
var name = "user-content-" + decodeURIComponent(location.hash.slice(1));
@dgraham
dgraham / gist:3750660
Created September 19, 2012 16:37
Hash -> Schema
require 'minitest/spec'
require 'minitest/autorun'
require 'set'
class Schemas
def initialize
@schemas = Set.new
end
def <<(hash)
@dgraham
dgraham / make-icns
Created February 8, 2015 00:26
png -> icns
if [ $# -ne 1 ]; then
echo "Usage: make-icns icon.png"
exit 1
fi
IMAGE=$1
OUT=`basename ${IMAGE%\.*}`.iconset
mkdir $OUT
sizes=(16 32 128 256 512)
@dgraham
dgraham / bench-function-pointer.c
Created April 15, 2017 18:27
Benchmark function pointer versus direct call.
#include <stdio.h>
#define MAX 100000000
int calculate() {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i;
}
return sum;
@dgraham
dgraham / const-expr.c
Created April 18, 2017 00:52
Constant expression inlining optimization.
// Compile without optimizations:
// cc -S -O0 -masm=intel const-expr.c
// Compile with optimizations:
// cc -S -O3 -masm=intel const-expr.c
int main() {
int x = 11;
int y = 12;
int z = x + y;
return z;
}
@dgraham
dgraham / template.js
Last active February 2, 2018 10:17
Minimum Viable Mustache
// $ traceur --experimental --out template-es6.js template.js
function escape(text) {
let el = document.createElement('p')
el.textContent = text
return el.innerHTML
}
function node(html) {
let parser = new DOMParser()