Clojure Macro
Last active
April 11, 2018 05:46
-
-
Save BadUncleX/7690efc4eac2fd3cf7fd to your computer and use it in GitHub Desktop.
Clojure Macro clj 宏
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
;; 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