Last active
August 21, 2018 21:14
-
-
Save fouric/6213a6046cfbe4aa213aef2f7dd66f1c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defmacro with-gethash-accessors (bindings &body body) | |
`(macrolet ,(mapcar (lambda (b) | |
(if (symbolp b) | |
(let ((sym (gensym)) | |
(keyword (intern (string b) :keyword))) | |
`(,b (,sym) `(gethash ,,keyword ,,sym))))) bindings) | |
,@body)) | |
;; target input: | |
(with-gethash-accessors (x y) | |
(x object) | |
(y object)) | |
;; desired output: | |
(macrolet ((x (#:gensym1) `(gethash :x ,#:gensym1)) | |
(y (#:gensym2) `(gethash :y ,#:gensym2))) | |
(x object) | |
(y object)) | |
;; actual output: | |
(macrolet ((x (#:gensym1) `(gethash ,:x ,#:gensym1)) | |
(y (#:gensym2) `(gethash ,:y ,#:gensym2))) | |
(x object) | |
(y object)) |
Shinmera
commented
Aug 21, 2018
•
flet
version
(defmacro with-gethash-accessors (bindings &body body)
`(flet ,(loop with obj = (gensym "OBJECT")
with val = (gensym "VALUE")
for var in bindings
for key = (intern (string var) :keyword)
collect `(,var (,obj) (gethash ',key ,obj))
collect `((setf ,var) (,val ,obj) (setf (gethash ',key ,obj) ,val)))
,@body))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment