I'm searching for a way to "pop" an element from an association list, in other words a "destructive" assoc:
(setq alist '((a . 1) (b . 2))
(assoc-pop 'a alist) ;; -> (a . 1)
;; alist -> ((b . 2))
Are there any function in the elisp harness? What's the most elegant way to obtain a symilar functionality? (not sure about that this sort of "side effect" is a good practice, even if it is possible!)
There is no such built-in operator that I am aware of, but I think that you can get this functionality quite quickly:
(defmacro assoc-pop (key alist)
`(let ((result (assoc ,key ,alist)))
(setq ,alist (delete result ,alist))
result))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With