Created
April 30, 2019 15:08
-
-
Save defunkydrummer/011c66f40a52977c2b33a1671bd5af0c to your computer and use it in GitHub Desktop.
Recursive getf to traverse nested plist structures
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
(defun getf* (l r) | |
"Recursive getf, to traverse nested plists. | |
Ej: | |
list = | |
(:id 10 :detail (:name \"Flavio\" :dni 666)) | |
(getf* list '(:detail :dni)) --> obtains 666 | |
it also works for lists of plists: | |
list = (:name "A" :list ((:id "myid1") (:id "myid2"))) | |
(getf* list '(:list :id)) | |
---> obtains ("myid1" "myid2") | |
" | |
(cond ((null l) nil) | |
((null r) l) ;nada que seleccionar -> devolver l | |
((and (consp l) | |
(keywordp (car l)) ; l es una PLIST | |
(keywordp (car r))); r todavia tiene keywords | |
(getf* (getf l (car r)) (cdr r))) | |
((and (consp l) | |
(consp (car l)) ; l es una lista de listas, no una PLIST | |
(keywordp (car r))); r todavia tiene keywords | |
;; aplicar para cada elemento de l y obtener resultados | |
(mapcar | |
(lambda (x) | |
(getf* (getf x (car r)) (cdr r))) | |
l)) | |
(t (error "getf* : implementation error")))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment