Skip to content

Instantly share code, notes, and snippets.

@djhaskin987
Last active July 31, 2016 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djhaskin987/a83d9a682a8f2fdc1d2e06fe62707c5c to your computer and use it in GitHub Desktop.
Save djhaskin987/a83d9a682a8f2fdc1d2e06fe62707c5c to your computer and use it in GitHub Desktop.
Useful Clojure Macros
(defn assoc-conj
"Conjoin a value onto the list corresponding to the key `k` in `mp`."
[mp k v]
(if (empty? mp)
{k [v]}
(if (empty? (mp k))
(assoc mp [v])
(assoc mp
(conj (mp k)
v)))))
(defmacro prefer [thing other]
`(let [x# ~thing]
(if x# x# ~other)))
(defmacro accept [form computation]
`(let [x# ~computation]
(match x#
~form x#
:else nil)))
(defmacro debug [form]
`(let [x# ~form]
(println
(str
"Debug: "
(quote ~form)
" is "
x#))
x#))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment