This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* { 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;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 |
OlderNewer