Created
March 25, 2020 21:20
-
-
Save adrianparvino/47ce40663f1e7fa07fc1bfd699292da7 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 access (trail value) | |
(let ((trail_ (reverse trail)) | |
(result value)) | |
(dolist (focus trail_ result) | |
(pcase focus | |
(`(vector . ,index) (setq result `(aref ,result ',index))) | |
(`(object . ,key) (setq result `(alist-get ',key ,result))))))) | |
(defmacro vector-alist-let (spec value body) | |
(let ((varlist '()) | |
(stack `((,spec . nil)))) | |
(while stack | |
(let* ((_ (pop stack)) | |
(focus (car _)) | |
(trail (cdr _))) | |
(cond | |
((listp focus) | |
(dolist (spec focus) | |
(cond | |
((listp spec) | |
(push `(,(cdr spec) . ((object . ,(car spec)) . ,trail)) stack)) | |
((symbolp spec) | |
(push `(,spec . ((object . ,spec) . ,trail)) stack))))) | |
((vectorp focus) | |
(let ((index 0)) | |
(mapc (lambda (elt) | |
(push `(,elt . ((vector . ,index) . ,trail)) stack) | |
(setq index (1+ index))) | |
focus))) | |
((symbolp focus) | |
(push `(,focus (access ,trail ,value)) varlist))))) | |
`(let ,varlist ,body))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment