Skip to content

Instantly share code, notes, and snippets.

View brehaut's full-sized avatar

Andrew Brehaut brehaut

View GitHub Profile
@brehaut
brehaut / gist:1685564
Created January 26, 2012 22:38
a middleware to remove utm_… params from query strings
(require '[clojure.string :as str])
(use '[ring.util.response :only [redirect]])
(defn remove-utm-params)
[query-string]
(->> (.split query-string "&")
(filter (fn [^String p] (.startsWith p "utm_")))
(str/join "&")))
(defn utm-redirect
;; original function
(defn assoc-meta [metable & kvs]
(with-meta metable (apply assoc (meta metable) kvs)))
;; rewritten with an explicit conjugation
(defn conjugate [translate untranslate f]
(comp untranslate f translate))
@brehaut
brehaut / multi.py
Created June 12, 2012 22:40
Minimal python multimethod implementation
"""A very simple, and minimal, multimethod implement in python inspired by clojure's multimethods
No support for default arguments, or hierarchies, and it relies on pythons default equality tests.
"""
class Multimethod(object):
def __init__(self, dispatch_fn):
self.dispatch_fn = dispatch_fn
self.handlers = {}
@brehaut
brehaut / picopico.js
Created July 10, 2012 23:43
a minimal parser combinator lib hauled out of a project
// parser
var _parser_global_state = null;
function ParseFailed () {};
function run_parser(p, input_list) {
input_list = input_list.slice()
try {
var old_parser_input = _parser_global_state;
@brehaut
brehaut / ref.roy
Created August 29, 2012 01:55
mutable refs in Roy
// the first hack needed to support mutable refs in Roy is allowing in place edits
// to JS arrays
let aSet (arr:[#a]) (idx:Number) (v:#a) :[#a] =
Array.prototype.splice.call arr idx 1 v
arr
// wrapper type is currently a structural type rather than an ADT because I couldn't
// get ADTs to have an Array as a field
type Ref = {_val: [#a]}
@brehaut
brehaut / on_drag.js
Created September 21, 2012 02:40
Trivial drag event registering function for jquery
function on_drag(el, handler) {
var el = $(el);
el.on("mousedown", function (e) {
var initial_x = e.clientY;
var initial_y = e.clientY;
function drag_handler(e) {
var delta_x = e.clientX - initial_x;
var delta_y = e.clientY - initial_y;
@brehaut
brehaut / lambdamanager.py
Created September 25, 2012 02:41
A django manager that takes a function to filter a queryset
from django.db import models
class LambdaManager(models.Manager):
"""LambdaManager is a simple manager extension that is instantiated with a callable.
This callable is passed the query set by get_query_set so that it can perform any
additional transformations to it such as eg filtering.
"""
def __init__(self, f):
@brehaut
brehaut / adminthumbnail.py
Created September 25, 2012 02:43
A function for generating admin_thumbnail methods for the django admin using sorl
from sorl.thumbnail import get_thumbnail
def make_thumbnail_method(get_fn=lambda o:o.image,
dimensions="100x60",
crop="center"):
"""A function factory for producing admin_thumbnail methods.
"""
def admin_thumbnail(self):
image = get_fn(self)
@brehaut
brehaut / typescriptalt.py
Last active December 18, 2015 22:38
Backporting the TypeScript filter to webassets 0.8 for use with tsc 0.9
"""This is an alternate typescript handler from future webassets as there
is a bug in the 0.8 version with recent tsc
"""
import os
import subprocess
import tempfile
from io import open # Give 2 and 3 use same newline behaviour.
from webassets.filter import Filter, register_filter
@brehaut
brehaut / gist:5880480
Last active December 19, 2015 02:08
generalized ridiculous greeting
(require '[clojure.string :as str])
(->> (map (comp rand-nth
(group-by first (seq (.split (slurp "/usr/share/dict/words") "\n")))
char)
[121 111 108 111])
(str/join " ")
print)