Skip to content

Instantly share code, notes, and snippets.

View jackrusher's full-sized avatar

Jack Rusher jackrusher

View GitHub Profile
@jackrusher
jackrusher / gist:2930841
Created June 14, 2012 14:56
Naive functional composition in Ruby
# Loads a directory's of files, each containing an eponymous function,
# in lexical order, then composes a function the calling of which will
# apply entire chain.
def compose(funcs)
funcs.map! { |f| f.to_proc }
if funcs.length == 2
# simple two function composition
lambda { |*args| funcs[0].call(funcs[1].call(*args)) }
@jackrusher
jackrusher / gist:3182488
Created July 26, 2012 14:49
Fetching PDF text with attributes in Clojure using PDFBox
(ns pdfbox.core
(:import [org.apache.pdfbox.pdmodel PDDocument]
[org.apache.pdfbox.util PDFMarkedContentExtractor TextPosition]
[java.util ArrayList]))
(defn parse-pdf [filename]
(let [pages (.getAllPages (.getDocumentCatalog (PDDocument/load filename)))
textpool (ArrayList.)
extract-text (proxy [PDFMarkedContentExtractor] []
(processTextPosition [text]
@jackrusher
jackrusher / gist:3562131
Created September 1, 2012 01:02
CSS to outline all elements on the page in depth-sensitive colors
* { outline: 1px dotted red; } * * { outline: 1px dotted green;} * * * { outline: 1px dotted orange;} * * * * { outline: 1px dotted blue;} * * * * * { outline: 1px solid fuchsia;} * * * * * * { outline: 1px solid teal;} * * * * * * * { outline: 1px solid indigo;} * * * * * * * * { outline: 1px solid brown;}
@jackrusher
jackrusher / Readme.md
Created November 11, 2012 16:21
Recursive nesting in Python and Clojure
@jackrusher
jackrusher / autotax.clj
Last active October 12, 2015 19:18
Autotaxer in Clojure
(defn load-taxonomy [file]
(reduce (fn [trie [k v]] (assoc-in trie k { :ssc v })) {}
(map (fn [line] (let [[path ssc] (string/split line #"\t")]
(list (string/split path #"_") ssc)))
(remove #(.startsWith % "#") (string/split-lines (slurp file))))))
(defn match-in-pool [taxonomy pool]
(if-let [this-match (taxonomy (first pool))]
(let [next-match (match-in-pool this-match (rest pool))]
(if (next-match :ssc)
@jackrusher
jackrusher / gist:4133648
Created November 23, 2012 01:42
NSMutableAttributedString in RubyMotion
# build attributed string to display the card
mutando = NSMutableAttributedString.alloc.initWithString(display_text)
# assign fonts and colors to regions of mutable string
[ /^(.*)$/, NSFontAttributeName, UIFont.fontWithName("Avenir", size: 16), # font for all
/^(.*)$/, NSForegroundColorAttributeName, "#696969".to_color, # color for all
/(^|\s|,)#([a-zA-Z0-9]*)/, NSForegroundColorAttributeName, "#444444".to_color, # tag
"\u00B6", NSForegroundColorAttributeName, "#e3e3e3".to_color # pilcrow
].each_slice(3).map do |rx,attr,style|
display_text.to_enum(:scan, rx)
generate_uuid = () ->
"aaaaaaaa-aaaa-4aaa-baaa-aaaaaaaaaaaa".replace /[ab]/g, (ch) ->
rnd = Math.random() * 16 | 0
out = (if ch is "a" then rnd else (rnd & 0x3 | 0x8))
out.toString 16
#
# Conforms to: http://tools.ietf.org/html/rfc4122
#
# 4.4. Algorithms for Creating a UUID from Truly Random or
@jackrusher
jackrusher / SimpleMarkovChain.clj
Last active September 25, 2017 18:51
A very simple implementation of Markov chaining in Clojure.
(defn model-from-sequence
"Returns a transition matrix of 'depth' from 'sequence'"
[depth sequence]
(loop [accum {} chunks (partition (inc depth) 1 (seq sequence))]
(if (seq? chunks)
(let [chunk (first chunks)
prefix (drop-last chunk)
suffix (last chunk)]
(recur (assoc accum prefix (conj (get accum prefix []) suffix)) (next chunks)))
accum)))
@jackrusher
jackrusher / FetchTweets.clj
Created December 24, 2012 17:41
Grab the last N tweets for user, where N is decided by Twitter's API limit.
(defn fetch-tweets
[user]
(loop [max-id nil accum []]
(let [fetch-str (str "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=" user "&count=1500" (when max-id (str "&max_id=" max-id)))
chunk (try (json/read-str (slurp fetch-str)) (catch Exception e))]
(if (or (nil? chunk) (< 200 (count chunk)))
(conj accum chunk)
(recur ((last chunk) "id") (conj accum chunk))))))
@jackrusher
jackrusher / decision_tree1.clj
Created February 12, 2013 21:59
A worked example for a friend showing how decision tree learning works.
;; Decision tree learning example taken from:
;; http://www.doc.ic.ac.uk/~sgc/teaching/pre2012/v231/lecture11.html
(defn log2
"log2(x), log2(0) = 0"
[x]
(if (= x 0) 0 (/ (Math/log x) (Math/log 2))))
;;;; Entropy
(defn entropy