Created
July 31, 2024 22:26
-
-
Save souenzzo/1f0d0fe4910f195c00d899e73dc326e5 to your computer and use it in GitHub Desktop.
guia de criação de macros
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
;; 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