Skip to content

Instantly share code, notes, and snippets.

@defunkydrummer
Last active November 25, 2020 21:32
Show Gist options
  • Save defunkydrummer/9233877a851a4ba97057f31f9edea836 to your computer and use it in GitHub Desktop.
Save defunkydrummer/9233877a851a4ba97057f31f9edea836 to your computer and use it in GitHub Desktop.
Helper for traversing ALISTs in depth
(defun geta (alist key)
"Similar to assoc-value... but operates in depth!
key can be a list or a single key."
(when (and alist key)
(etypecase key
((or string symbol)
(alexandria:assoc-value alist key :test 'string=))
(cons
;; associate in depth
(if (cdr key)
(geta (alexandria:assoc-value alist (car key) :test 'string=)
(cdr key))
;; else: only one key in list.
(alexandria:assoc-value alist (car key) :test 'string=))))))
@defunkydrummer
Copy link
Author

defunkydrummer commented Nov 25, 2020

Note: This assumes the alexandria library is loaded.
Of course, assoc-value is the same as (cdr (assoc key alist :test ...))

Of course this can be improved so you pass the test function to use, and remove the dependency on alexandria. I am posting this as a quick and dirty way to traverse the alists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment