Skip to content

Instantly share code, notes, and snippets.

View davidchambers's full-sized avatar

David Chambers davidchambers

View GitHub Profile
@davidchambers
davidchambers / ismodule
Created November 4, 2013 01:58
Determine whether a JavaScript file is an AMD module
#!/usr/bin/env coffee
esprima = require 'esprima'
isModule = (source) ->
esprima.parse(source).body.some (node) ->
node.type is 'ExpressionStatement' and
node.expression.type is 'CallExpression' and
node.expression.callee.type is 'Identifier' and
concat = (lists...) ->
[].concat(lists...)
_traverse = (list, path, f) ->
if list.length is 0
f(path)
else
for item, idx in list
_traverse(concat(list.slice(0, idx), list.slice(idx + 1)),
concat(path, [item]), f)
@davidchambers
davidchambers / DETAILS
Last active December 25, 2015 17:59
Coldsnap Constructed
DATE
TBD. Probably 2014-01-04 or 2014-01-05.
VENUE
TBD. Somewhere in Auckland, New Zealand.
@davidchambers
davidchambers / example.py
Created October 16, 2013 18:16
The right approach to memoization: keep it separate
import time
def memoize(f):
memo = {}
def wrapper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return wrapper
@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
FILES = (
'foo.txt',
'bar.txt',
'baz.txt',
)
def getmetadata(path):
return {'blah': True, 'quux': 42}
@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'
@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
# 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) ->