Skip to content

Instantly share code, notes, and snippets.

View brehaut's full-sized avatar

Andrew Brehaut brehaut

View GitHub Profile
@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 = {}
;; 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 / 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
ifr = document.createElement("iframe");
document.body.appendChild(ifr);
ifr.contentDocument.write("<script language='javascript'>window.Object2 = Object;</script>");
o = new ifr.contentWindow.Object2();
o instanceof Object; // => false
@brehaut
brehaut / apropos-ns.clj
Created November 29, 2011 01:24
apropos that returns a seq of symbols that include their namespace
;; hacked out of clojure.repl/apropos and prepending namespaces
(defn apropos-ns
"Given a regular expression or stringable thing, return a seq of
all definitions in all currently-loaded namespaces that match the
str-or-pattern."
[str-or-pattern]
(let [matches? (if (instance? java.util.regex.Pattern str-or-pattern)
#(re-find str-or-pattern (str %))
#(.contains (str %) (str str-or-pattern)))]
@brehaut
brehaut / lsystem.js
Created November 9, 2011 01:17
l-system generator
function l_system (productions, depth, s) {
if (depth === 0) return s;
return $.map(s, function (v) {
return l_system(productions, depth - 1, productions(v) || [v]);
});
}
function run_turtle (turtle, operations, commands) {
$.each(commands, function (_, c) {
(operations[c] || $.noop)(turtle);
; archaic, an alternative to https://gist.github.com/1343842
(defn temp-gr
[start by end]
(fn [x]
(when (<= start x end)
(let [lower (* (quot x by) by)
upper (+ lower by)]
(keyword (str lower "-" upper))))))
@brehaut
brehaut / populate.py
Created June 28, 2011 23:18
Script to generate a mock directory of media based on a listing
"""populate.py
This script takes a listing of folders and filenames (as generated by `ls -R > listing`) on standard input
and the pathname for a set of media as the argument to argv.
e.g:
python populate.py _source < test/listing.txt
The files listed in listing are then mocked from the media files using the filenames in the input, and
@brehaut
brehaut / iphone-window.js
Created June 20, 2011 04:41
Bookmarklet that opens the current page in an iphone sized window
/*
javascript:%28function%20%28%29%20%7B%0A%20%20var%20w%20%3D%20window.open%28window.location%2C%20window.title%2C%20false%29%3B%0A%20%20w.resizeTo%28320%2C480%29%3B%0A%20%20w.onload%20%3D%20function%20%28%29%20%7B%0A%20%20%20%20var%20b%20%3D%20w.document.body%3B%0A%20%20%20%20b.style.overflow%20%3D%20%22scroll%22%3B%0A%20%20%20%20w.resizeBy%28320%20-%20b.offsetWidth%2C%200%29%3B%0A%20%20%7D%3B%0A%7D%29%28%29%3B
*/
(function () {
var w = window.open(window.location, window.title, false);
w.resizeTo(320,480);
w.onload = function () {
var b = w.document.body;
b.style.overflow = "scroll";
(->> (re-seq #"(?i)(\d+)\.|(yes|no)" "no1. asdf\n\nasdf foo yes2.\n\nfsdf\nno yes no")
(map rest)
(partition-by first)
(drop-while (comp nil? ffirst))
(partition 2)
(map (fn [s] (let [[i & r] (keep identity (flatten s))]
[(Integer/parseInt i)
(-> (last r) .toLowerCase first (= \y))])))
(into {}))