Skip to content

Instantly share code, notes, and snippets.

View cwvh's full-sized avatar

Chris Van Horne cwvh

  • Seattle, WA, USA
View GitHub Profile
@cwvh
cwvh / images2gif.py
Created December 1, 2011 04:01
Take a list of PIL-supported images and turn them into a gif meme.
# -*- coding: utf-8 -*-
# Copyright (c) 2010, Almar Klein, Ant1, Marius van Voorden
#
# This code is subject to the (new) BSD license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
@cwvh
cwvh / scopelift.rb
Created December 2, 2011 07:04
The Hoarder Pattern: beautiful on the outside, dead kittens on the inside. Thanks Sinatra!
## Making kick-ass global objects for fun-and-profit DSLs.
module ScopeLifting
class Application
attr_accessor :options
def initialize
@options = Hash.new
end
@cwvh
cwvh / namedmatch.py
Created December 9, 2011 03:00
Clean code with better named captures.
"""`namedmatch` mimics `re.match` but also creates attributes based on the
values of the named captures.
Example
>>> string = 'foo bar baz'
>>> match = namedmatch(r'(?P<start>\w+) (?P<end>\w+)', string)
>>> print match.start
"foo"
>>> print match.end
@cwvh
cwvh / game.py
Created December 9, 2011 05:51
Example of asymmetric and symmetric multimethods.
from mm import multimethod, symmultimethod
class Ship(object):
def __init__(self, name):
self.name = name
def __str__(self):
return self.name
class Asteroid(object):
def __init__(self, name):
@cwvh
cwvh / parse.py
Created December 10, 2011 02:26
Simple parsing with pretty decorators.
import os
import re
class Env(object):
"""Maintains the environment variables for local and global scope."""
def __init__(self, env={}, parent=os.environ):
self.env = env.copy()
self.parent = parent.copy()
@cwvh
cwvh / foo.py
Created December 11, 2011 21:47
from pipeline import pipeline
@pipeline()
def foo():
pass
@pipeline()
def baz():
pass
@cwvh
cwvh / gist:1495096
Created December 19, 2011 01:55
stash
@pattern(r'^([a-zA-Z_\$\%][a-zA-Z_0-9\{\}]*)\s*:(?<=:)(?::([a-zA-Z_\$][a-zA-Z_0-9\{\}]*)?:)?\s*(.*)$')
def target(match, env=env):
labels = ('name', 'dep', 'components')
groups = dict(zip(labels, match.groups()))
for k, v in groups.iteritems():
groups[k] = env.interp(v) if v else v
name, dep = groups['name'], groups['dep']
components = re.findall('\w+', groups['components'])
makefile.addtarget(name, dep, components)
#!/usr/bin/env bash
# $ gostart x
# vim x.go
FILENAME="$1"
cp "$GOROOT"/doc/Makefile .
sed -i '' -e "s/tmpltohtml/"$FILENAME"/g" -e 's/\.\./$\(GOROOT\)/' -e '/^#.*/d' Makefile
touch "$1".go
class Comonad w where
(=>>) :: w a -> (w a -> b) -> w b
extract :: w a -> a
data T a = L a | B (T a) a (T a) deriving (Eq, Show)
unit :: a -> T a
unit = L
counit :: T a -> a
@cwvh
cwvh / A.hs
Created May 15, 2014 21:34
bifunctors
data Gabuzomeu a b = Gabu a | Zomeu b
deriving Show
shadok :: (a -> c) -> (b -> d) -> Gabuzomeu a b -> Gabuzomeu c d
shadok f _ (Gabu a) = Gabu (f a)
shadok _ g (Zomeu b) = Zomeu (g b)
newtype G b a = G {first :: Gabuzomeu a b}
deriving Show