Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popping an element from an association list in lisp (elisp)

Tags:

emacs

lisp

elisp

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!)

like image 970
pygabriel Avatar asked Oct 15 '22 01:10

pygabriel


1 Answers

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))
like image 150
Svante Avatar answered Oct 20 '22 18:10

Svante