Skip to content

Instantly share code, notes, and snippets.

@viebel
Created July 14, 2016 07:03
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 viebel/0d5e62307e808024d88f63e40ba04eee to your computer and use it in GitHub Desktop.
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.
(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