On 22 Sep 2013, at 17:24, Faré fahree@gmail.com wrote:
On Sun, Sep 22, 2013 at 9:53 AM, Pascal Costanza pc@p-cos.net wrote:
(defmacro assure (type form) (let ((object (copy-symbol 'object))) `(let ((,object ,form)) (check-type ,object ,type) ,object)))
If Alexandria doesn't want it, the problem is that there doesn't seem to be any widespread enough library for general utilities that moves at decent speed http://xkcd.com/927/
Well, I hope someone's listening. ;)
(defmacro assocf (item alist &optional default &rest keys &key test test-not key) (declare (ignore test test-not key)) (let ((it (copy-symbol 'it)) (cons (copy-symbol 'cons))) `(let* ((,it ,item) (,cons (assoc ,it ,alist ,@keys))) (unless ,cons (setf ,cons (cons ,it ,default) ,alist (cons ,cons ,alist))) ,cons)))
This implementation loses badly if the alist form has side-effects. That where you'd use the long form of define-modify-macro.
Good point. This should be better:
(defmacro assocf (item alist &optional default &rest keys &key test test-not key &environment env) (declare (ignore test test-not key)) (let ((it (copy-symbol 'it)) (cons (copy-symbol 'cons))) (multiple-value-bind (vars vals store-vars writer reader) (get-setf-expansion alist env) (assert (null (cdr store-vars))) `(let* ((,it ,item) ,@(mapcar 'list vars vals) (,cons (assoc ,it ,reader ,@keys))) (unless ,cons (setq ,cons (cons ,it ,default)) (let ((,(car store-vars) (cons ,cons ,reader))) ,writer)) ,cons))))
Also, assocf is a slightly confusing name considering what you usually expect from define-modify-macro.
I'm open for better suggestions. I chose assocf because it reminds me of getf.
Pascal
-- Pascal Costanza The views expressed in this email are my own, and not those of my employer.