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
(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 |
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
; 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))) |
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 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 |
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 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))) |
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
;; 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])) |
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
(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))) |
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
;;;; 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)) |
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 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) |
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 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) |
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
(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#))))) |
OlderNewer