[alexandria-devel] setf cdr-assoc

John Fremlin john at fremlin.org
Fri Jan 23 15:01:01 UTC 2009


Sometimes it's convenient to store things in a list like '((key . value)
(another-key . value)) rather than a full blown (and unordered) hash
table.

It is available now in cl-irregsexp.utils but could you add the fairly
standard cdr-assoc function to alexandria?


(declaim (inline cdr-assoc))
(defun cdr-assoc (alist key &key (test 'eql))
  (cdr (assoc key alist :test test)))
(define-setf-expander cdr-assoc (place key &key (test ''eql) &environment env)
  (multiple-value-bind (dummies vals newval setter getter)
      (get-setf-expansion place env)
    (with-unique-names (store key-val test-val alist found)
      (values 
       (append dummies (list key-val test-val))
       (append vals (list key test))
       `(,store , at newval)
       `(let (,@(mapcar 'list dummies vals)
	      (,alist ,getter))
	  (let ((,found (assoc ,key-val ,alist :test ,test-val)))
	    (cond (,found 
		   (setf (cdr ,found) ,store))
		  (t
		   (setf ,(first newval) (acons ,key ,store ,alist))
		   ,setter))
	    ,store))
       `(cdr-assoc ,getter)))))




More information about the alexandria-devel mailing list