Skip to content

Instantly share code, notes, and snippets.

View jackrusher's full-sized avatar

Jack Rusher jackrusher

View GitHub Profile
(ns workbook.dec-tree)
;; An example drawn 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))))
@jackrusher
jackrusher / decision-tree-to-match.clj
Created February 22, 2013 16:49
Convert a learnt decision tree into a nice, readable core.match program.
;; Converts a learnt decision tree produced by
;; https://gist.github.com/jackrusher/4971531
;; into a core.match program. A nice way to convert a set of data points into a
;; readable set of rules. I haven't looked at the code, but I assume core.match
;; compiles the rules into a decision tree.
(defn tree-to-match-clauses [t p]
(let [children (keys t)]
(if (keyword? (first children))
(conj p (first (vals t)))
@jackrusher
jackrusher / triples.clj
Last active December 14, 2015 02:59
A super simple example of inference from a set of triples.
;; knowledge representation, syllogism via knowledge base
(defrecord Triple [subject predicate object])
(def knowledge-base
[(Triple. :Socrates :is-a :Greek)
(Triple. :Greek :is-a :human-being)
(Triple. :human-being :has-property :mortal)])
(defn collect-predicate-along-axis [kb subject predicate axis]
@jackrusher
jackrusher / log-ws.clj
Created March 8, 2013 03:15
Aleph/Lamina makes it crazy easy to log events using WebSocket in clojure.
(use 'lamina.core 'aleph.http)
(defn log-handler [ch handshake]
(receive-all ch #(spit "event.log" % :append true)))
(def server
(start-http-server log-handler {:port 6969 :websocket true}))
@jackrusher
jackrusher / gist:5139396
Last active March 29, 2023 21:02
Hofstadter on Lisp: Atoms and Lists, re-printed in Metamagical Themas.

Hofstadter on Lisp

In the mid-80s, while reading through my roommate's collection of Scientific American back issues, I encountered this introduction to Lisp written by Douglas Hofstadter. I found it very charming at the time, and provide it here (somewhat illegally) for the edification of a new generation of Lispers.

In a testament to the timelessness of Lisp, you can still run all the examples below in emacs if you install these aliases:

(defalias 'plus #'+)
(defalias 'quotient #'/)
(defalias 'times #'*)
(defalias 'difference #'-)

Shannon's entropy calculation

Shannon's measure of entropy is essentially a measure of the unpredictability of a signal that has been converted into a stream of samples. His work was initially concerned with efficient representation of a signal over a network of switching relays, but it has turned out to be of great value in many areas of computer science. Applications include error correcting codes for noisy channels (how many extra bits does it take to tolerate a given level of noise?), data compression (what is the least number of bits that can represent

@jackrusher
jackrusher / entropy2.clj
Last active December 15, 2015 16:29
Part II on entropy, working toward the ID3 algorithm.
;; Fill in this definition of seq-entropy that takes a second
;; parameter that's the function that returns 'true' when a given item
;; is positive.
(defn seq-entropy
"Calculate the entropy of sequence 'sq' assuming the all positive
numbers represent positive samples."
[sq pos-func]
;; ...
)
;; Homework III
;; first, some useful functions from the standard library...
(frequencies [1 2 1 2 1 2 3 4 2 3 4 5])
;; => {1 3, 2 4, 3 2, 4 2, 5 1}
;; a convenience function that returns a map from each value in the
;; input to the count of its occurence
;; Evan, here's the python equivalent:
(ns tumblr-backup.core
(:use [clojure.xml :as xml]))
(defn build-tumblr-url [start howmany]
(format "http://blog.jackrusher.com/api/read/?start=%d&num=%d" start howmany))
(defn get-post-count []
(read-string (-> (xml/parse (build-tumblr-url 0 0)) :content second :attrs :total)))
(defn spit-post [post]
@jackrusher
jackrusher / abstract.md
Last active February 14, 2024 16:31
An old programming koan.

The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."

Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and