Skip to content

Instantly share code, notes, and snippets.

@BadUncleX
Last active April 11, 2018 05:46
Show Gist options
  • Save BadUncleX/7690efc4eac2fd3cf7fd to your computer and use it in GitHub Desktop.
Save BadUncleX/7690efc4eac2fd3cf7fd to your computer and use it in GitHub Desktop.
Clojure Macro clj 宏
;; from braveclojure
;; http://www.braveclojure.com/writing-macros/#3_3__Syntax_Quoting
(defn criticize-code
[criticism code]
`(println ~criticism (quote ~code)))
(defmacro code-critic
[{:keys [good bad]}]
`(do ~(criticize-code "Cursed bacteria of Liberia, this is bad code:" bad)
~(criticize-code "Sweet sacred boa of Western and Eastern Samoa, this is good code:" good)))
;; refactor
(defmacro code-critic
[{:keys [good bad]}]
`(do ~@(map #(apply criticize-code %)
[["Sweet lion of Zion, this is bad code:" bad]
["Great cow of Moscow, this is good code:" good]])))
(code-critic {:good (+ 1 1) :bad (1 + 1)})
; =>
; Sweet lion of Zion, this is bad code: (1 + 1)
; Great cow of Moscow, this is good code: (+ 1 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment