Skip to content

Instantly share code, notes, and snippets.

View brehaut's full-sized avatar

Andrew Brehaut brehaut

View GitHub Profile
@brehaut
brehaut / Open all selected links
Created November 4, 2010 23:46
A bookmarklet to open all the links in the current selection in new windows/tabs.
@brehaut
brehaut / pivot-on.clj
Created February 3, 2011 02:58
a scruffy implementation of a function to pivot on a predicate
(defn pivot-on
"This is about the worst way you could ever write this"
[p coll]
(first (reduce (fn [[[l r] pivoted] v]
(if pivoted
[[l (conj r v)] true]
(if (p v)
[[l r] true]
[[(conj l v) r] false])))
[[[] []] false]
@brehaut
brehaut / fix-parens.clj
Created March 10, 2011 21:48
fix-parens; a function to correct bracket types
(use 'name.choi.joshua.fnparse)
(def tokenize (partial re-seq #"\\[\[\](){}]|\"(?:\\.|[^\"])*\"|[\[\](){}]|[^\[\](){}]+"))
(def initial-state (comp (partial array-map :remainder) tokenize))
(def brackets {"[" "]" "(" ")" "{" "}"})
(def opening (term (set (keys brackets))))
(->> (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 {}))
@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";
@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
; 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 / 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);
@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)))]
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