[alexandria-devel] remove-from-plist

Michael Weber michaelw+alexandria at foldr.org
Tue Mar 11 13:15:07 UTC 2008


Below are three different implementations of remove-from-plist, all  
of which I find more aestetic than current alexandria's  
implementation.  YMMV.

I would favor the first one, which explicitly ditches error checking  
("it's user responsibility", and a proper plist will be returned).

In case somebody disagrees, the second implementation reintroduces  
it, albeit with a better error message.

The third implementation checks for properness only at the end  
(outside the loop), while still being shorter than alexandria's  
implementation.

I would also remove the FIXME, on grounds that if plists are long  
enough for that to matter, one shouldn't be using them anyway.

(defun remove-from-plist (plist &rest keys)
   (loop for (key value) on plist by #'cddr
         unless (member key keys :test #'eq)
         collect key and collect value))

(defun remove-from-plist (plist &rest keys)
   (loop for (key . rest) on plist by #'cddr
         do (assert rest () "Expected a proper plist, got ~S" plist)
         unless (member key keys :test #'eq)
         collect key and collect (first rest)))

(defun remove-from-plist (plist &rest keys)
   (loop with last = t
         for (key . rest) on plist by #'cddr
         do (setf last rest)
         unless (member key keys :test #'eq)
         collect key and collect (first rest)
         finally (assert last () "Expected a proper plist, got ~S"  
plist)))


Cheers,
Michael





More information about the alexandria-devel mailing list