Skip to content

Instantly share code, notes, and snippets.

@defunkydrummer
Created April 30, 2019 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defunkydrummer/011c66f40a52977c2b33a1671bd5af0c to your computer and use it in GitHub Desktop.
Save defunkydrummer/011c66f40a52977c2b33a1671bd5af0c to your computer and use it in GitHub Desktop.
Recursive getf to traverse nested plist structures
(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