Skip to content

Instantly share code, notes, and snippets.

View timmc's full-sized avatar

Tim McCormack timmc

View GitHub Profile
@timmc
timmc / defref.clj
Created February 12, 2011 01:41
How should I unit test code that uses globally-defined refs?
(def foo (ref 5))
(def bar (ref 7))
(def stuff (ref nil))
(defn update-stuff! [x]
(dosync
(ref-set stuff (* x @foo @bar))))
; but this is hard to unit-test
; I needed a way to call a sequence of functions until one returned false,
; but return the last true return.
(defn changes
"Call functions sequentially until one returns false; return true if any returned true. Not tail-recursive."
([] false)
([f] (f))
([f & fs]
(let [fv (f)]
(when fv (do (apply changes fs)))
(defn pop-lax
"Pop the vector, but given an empty vector, simply return it."
[v]
(if (seq v) (pop v) []))
(def ^{:doc "Add a vertex to the last curve."}
append-vertex!
^{:actname "add vertex"}
(fn [^Point2D wp]
(dosync
(defn replace-1
"Replace the first x in coll such that (pred x) is true with (gen x).
Not tail-recursive."
[coll pred gen]
(if (seq coll)
(if (pred (first coll))
(conj (rest coll)
(gen (first coll)))
(conj (replace-1 (rest coll) pred gen)
(first coll)))
;; Original:
((fn[[?<&,%*+,$*|]](?<&,%*+,$*|))({[^:<?>,'*$][+(+(*)(*))(*,3(*))]}['*$]))
;; Without gratuitous use of non-alphanumerics in symbols
((fn[[a,b,c]](a,b,c))({[^:x,'y][+(+(*)(*))(*,3(*))]}['y]))
;; and spacing normalized
((fn [[a b c]] (a b c)) ({[^:x 'y] [+ (+ (*) (*)) (* 3 (*))]} ['y]))
@timmc
timmc / serialization.clj
Created March 6, 2011 05:08
I thought defrecords were working OK
(defrecord Foo [a b])
;; => user.Foo
(def example (Foo. 3 (sorted-map :hello 5)))
;; => #'user/example
example
;; => #:user.Foo{:a 3, :b {:hello 5}}
(def serialized (binding [*print-dup* true] (pr-str example)))
;;;; fnparse semantics
(defn as-decimal-number
"Read a sequence of characters as a decimal integer."
[[& digit-chars]]
(Integer/parseInt (apply str digit-chars) 10))
;;;; basic token classes
(def zero-digit (lit \0))
@timmc
timmc / gist:875355
Created March 17, 2011 23:23
Naughty stuff with classes?
(ns org.timmc.mipsiss.instructions
"Abstract MIPS instruction representation.")
;; Text formats:
;; - :s is RS e.g. $r9
;; - :t is RT e.g. $r9
;; - :d is RD e.g. $r9
;; - :C is Imm e.g. 8
;; - :b is base (RS) e.g. ($r9)
(ns org.timmc.mipsiss.instructions
"Abstract MIPS instruction representation.")
;; Text formats:
;; - :s is RS e.g. $r9
;; - :t is RT e.g. $r9
;; - :d is RD e.g. $r9
;; - :C is Imm e.g. 8
;; - :o is offset and base (RS) e.g. -4($r9)
@timmc
timmc / gist:897116
Created March 31, 2011 19:54
Sample macro to create multiple functions
(defmacro defconv
[a-symb b-symb num-expr]
(when-not (and (symbol? a-symb) (symbol? b-symb))
(throw (Exception. "defconv not given two symbols")))
(let [ab-symb (symbol (str (name a-symb) "->" (name b-symb)))
ba-symb (symbol (str (name b-symb) "->" (name a-symb)))]
`(let [num# ~num-expr]
(defn ~ab-symb [~'a] (* ~'a num#))
(defn ~ba-symb [~'b] (/ ~'b num#)))))