assert / check-type / assure...
Pascal Costanza
pc at p-cos.net
Sun Sep 22 16:57:05 UTC 2013
On 22 Sep 2013, at 17:24, Faré <fahree at gmail.com> wrote:
> On Sun, Sep 22, 2013 at 9:53 AM, Pascal Costanza <pc at 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 , at 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 , at 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.
More information about the pro
mailing list