Last active
July 31, 2016 05:51
-
-
Save djhaskin987/a83d9a682a8f2fdc1d2e06fe62707c5c to your computer and use it in GitHub Desktop.
Useful Clojure Macros
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 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))))) |
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 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