Instantly share code, notes, and snippets.

# Tim McCormacktimmc

• Sort options
Created Feb 12, 2011
How should I unit test code that uses globally-defined refs?
View defref.clj
 (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
Created Feb 16, 2011
View changes.clj
 ; 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)))
Created Feb 19, 2011
View gist:835284
 (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
Created Mar 3, 2011
View gist:853113
 (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)))
Created Mar 4, 2011
View gist:855925
 ;; 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]))
Created Mar 6, 2011
I thought defrecords were working OK
View serialization.clj
 (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)))
Created Mar 17, 2011
View gist:875176
 ;;;; 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))
Created Mar 17, 2011
Naughty stuff with classes?
View gist:875355
 (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)
Created Mar 19, 2011
View instructions.clj
 (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)
Created Mar 31, 2011
Sample macro to create multiple functions
View gist:897116
 (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#)))))
You can’t perform that action at this time.