[alexandria-devel] cdr assoc (now called alist-get)

John Fremlin john at fremlin.org
Sun May 24 10:47:47 UTC 2009


Dear Alexandrians,

"Tobias C. Rittweiler" <tcr at freebits.de> writes:
>> I hope that was why you Alexandrians ignored me . . .
>
> No ignoring, it's just the wrong phase of the moon right now.

Please consider the cdr-assoc again. It could be improved, which
improvements would encourage you to view it more favourably?

It is pretty useful to be able to use alists as an alternative to a
small hash table.

(declaim (inline racons))
(defun racons (key value ralist)
  (acons value key ralist))


(macrolet ((define-alist-get (name get-pair get-value-from-pair add)
	     `(progn
		(declaim (inline ,name))
		(defun ,name (alist key &key (test 'eql))
		  (let ((pair (,get-pair key alist :test test)))
		    (values (,get-value-from-pair pair) pair)))
		(define-setf-expander ,name (place key &key (test ''eql)
					     &environment env)
		  (multiple-value-bind (dummies vals newvals setter getter)
		      (get-setf-expansion place env)
		    (when (cdr newvals)
		      (error "~A cannot store multiple values in one place" ',name))
		    (with-unique-names (store key-val test-val alist found)
		      (values 
		       (append dummies (list key-val test-val))
		       (append vals (list key test))
		       (list store)
		       `(let ((,alist ,getter))
			  (let ((,found (,',get-pair ,key-val ,alist :test ,test-val)))
			    (cond (,found 
				   (setf (,',get-value-from-pair ,found) ,store))
				  (t
				   (let ,newvals
				     (setf ,(first newvals) (,',add ,key ,store ,alist))
				     ,setter)))
			    ,store))
		       `(,',name ,getter ,key))))))))
  (define-alist-get alist-get assoc cdr acons)
  (define-alist-get ralist-get rassoc car racons))

[...]





More information about the alexandria-devel mailing list