Skip to content

Instantly share code, notes, and snippets.

View davidchambers's full-sized avatar

David Chambers davidchambers

View GitHub Profile
@davidchambers
davidchambers / decorate.coffee
Created December 10, 2012 23:19
Effortlessly extend class methods in CoffeeScript
decorate = (klass, methodName, fn) ->
method = klass::[methodName]
klass::[methodName] = (args...) ->
method.apply this, args
fn.apply this, args
# Example
decorate Array, 'push', (args...) ->
@davidchambers
davidchambers / cuti.py
Last active December 11, 2015 02:58
Print known Uniform Type Identifiers as JSON
#!/usr/bin/env python
import json
import re
import subprocess
import sys
if len(sys.argv) > 1:
lsregister = sys.argv[1]
else:

A woman has two children, (at least) one of whom is a boy. What's the probability that she has two boys?

       B    B-B   1/4
      /
     /
    B
   / \
  /   \
 /     G    B-G   1/4

/

flatter

flatter is a command-line tool for determining a source file's "indentedness", which provides a rough indication of how well factored the code is.

$ flatter src
cheerio = require 'cheerio'
request = require 'request'
tutor = require 'tutor'
request 'http://www.wizards.com/magic/tcg/events.aspx?x=mtg/event/playerschamp/2012/cubepool',
(err, res, body) ->
throw err if err?
$ = cheerio.load body
names = $('a.nodec').map -> @text()
# Takes a box (an array) and a function, f, which takes a continuation.
# A sentinel is placed in the box, then f is invoked. When f calls its
# continuation, c, the box is inspected. Only if the sentinel is still
# in the box will c in turn call its continuation.
#
# If several asynchronous requests are made in quick succession, the
# responses may not be received in the order in which they were sent.
# This function makes it possible to define a function to be invoked
# *unless the request has been superseded by the time it completes*.
skip_continuation_if_superseded = (box, f) ->
@davidchambers
davidchambers / values.md
Created August 7, 2013 18:01
Engineering values
  • avoid unnecessary state
  • favor pure functions over impure functions
  • functions should not have surprising side effects
  • give things meaningful names, and avoid aliases
  • don't swallow unexpected errors
@davidchambers
davidchambers / app.coffee
Last active December 20, 2015 19:09
Express routing using regular expressions with named capturing groups
express = require 'express'
app = express()
app.param (name, arg) -> switch Object::toString.call arg
when '[object RegExp]'
(req, res, next, val) ->
if arg.test val then next() else next 'route'
FILES = (
'foo.txt',
'bar.txt',
'baz.txt',
)
def getmetadata(path):
return {'blah': True, 'quux': 42}
@davidchambers
davidchambers / RESULTS
Last active December 23, 2015 01:59
Weatherlight Constructed
ROUND 1
Darryn 2 v 0 James
Dan 2 v 0 Matt
Doug 0 v 2 David
ROUND 2
Darryn 2 v 0 David
James 2 v 1 Dan