Created
July 14, 2016 07:03
-
-
Save viebel/0d5e62307e808024d88f63e40ba04eee to your computer and use it in GitHub Desktop.
The defprint macro defines a function with a tweak: each time the function is called, it prints the values of its arguments. The tricky part is that it works also with desctructuring.
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 my.best$macros) | |
(defmacro defprint1 [func-name args & body] | |
`(defn ~func-name [& args#] | |
(print '~func-name "called with: " args#) | |
(let [~args args#] | |
~@body))) | |
(defmacro defprint2 | |
[name args & body] | |
`(defn ~name [~@args] | |
(do | |
(print '~name "called with:" ~args) | |
~@body))) | |
(defmacro defprint3 | |
[name args & body] | |
`(defn ~name ~args | |
(do | |
(print '~name "called with:" ~args) | |
~@body))) | |
(my.best/defprint1 hello-world-1 [& {:keys [language] | |
:or {language :en}}] | |
(case language | |
:fr "bonjour monde" | |
:en "hello world")) | |
(my.best/defprint2 hello-world-2 [& {:keys [language] | |
:or {language :en}}] | |
(case language | |
:fr "bonjour monde" | |
:en "hello world")) | |
(my.best/defprint3 hello-world-3 [& {:keys [language] | |
:or {language :en}}] | |
(case language | |
:fr "bonjour monde" | |
:en "hello world")) | |
[(with-out-str (hello-world-1 :language :fr)) | |
(with-out-str (hello-world-2 :language :fr)) | |
(with-out-str (hello-world-3 :language :fr))] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment