Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created July 31, 2024 22:26
Show Gist options
  • Save souenzzo/1f0d0fe4910f195c00d899e73dc326e5 to your computer and use it in GitHub Desktop.
Save souenzzo/1f0d0fe4910f195c00d899e73dc326e5 to your computer and use it in GitHub Desktop.
guia de criação de macros
;; escreva uma função que recebe o listas e simbolos como argumento,
;; e faca as transformacoes
(defn my-with-open-fn
[bindings body]
`(let ~bindings
(try
~@body
(finally
~@(for [v (map first (partition-all 2 bindings))]
`(.close ~v))))))
;; teste usando listas e simbolos
(my-with-open-fn '[a (start!)]
'((my-code on a)))
;; => (clojure.core/let [a (start!)] (try (my-code on a) (finally (.close a))))
;; parece correto!
;; crie a macro "de verdade"
(defmacro my-with-open [bindings & body]
(my-with-open-fn bindings body))
;; verifique a macro "de verdade" usando macroexpand
(macroexpand '(my-with-open [hello (world)]
(apply hello world)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment