Skip to content

Instantly share code, notes, and snippets.

View dgraham's full-sized avatar
💭

David Graham dgraham

💭
View GitHub Profile
@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 / osx-setup.sh
Last active August 16, 2024 01:21
A setup script for macOS development.
xcode-select --install
if [ ! -x /opt/homebrew/bin/brew ]; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
brew tap 'homebrew/bundle'
brew tap 'homebrew/services'
brew install automake clang-format cmake icu4c libressl libxml2 openssl pkg-config readline sqlite yajl
@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 / range-set.rb
Created November 12, 2021 23:55
Merge intersecting ranges
require "minitest/autorun"
class RangeSet
include Enumerable
def initialize(ranges = [])
@ranges = merge(ranges)
end
def <<(range)
@dgraham
dgraham / git-cowork
Created September 8, 2023 23:18
git cowork
#!/bin/sh
if [ "$1" = "--stop" ]
then
git config --unset-all cowork.author
elif [ -n "$1" ]
then
author=$(git log --no-merges --author "$1" -1 --pretty="%an <%ae>")
if [ -n "$author" ]
then