Skip to content

Instantly share code, notes, and snippets.

Tim McCormack timmc

Block or report user

Report or block timmc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@timmc
timmc / defref.clj
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
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)))
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
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)))
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]))
@timmc
timmc / serialization.clj
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)))
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))
@timmc
timmc / gist:875355
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)
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)
@timmc
timmc / gist:897116
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.