Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to merge plists?

Is there an easy way in Common Lisp to merge two plists? Or from another point of view: is there a way to remove duplicates from a plist? I know I can just append plists (and GETF will take the first one it finds), but I'd like to not keep accumulating unused keys as my app runs.

I'm thinking about something like (loop for p on my-plist by #'cddr ...), but there's often an easier way than my first thought!

like image 581
Ken Avatar asked Aug 03 '10 16:08

Ken


1 Answers

You could start from this primitive version:

(defun merge-plist (p1 p2)
  (loop with notfound = '#:notfound
        for (indicator value) on p1 by #'cddr
        when (eq (getf p2 indicator notfound) notfound) 
        do (progn
             (push value p2)
             (push indicator p2)))
  p2)

CL-USER 104 > (merge-plist '(a 1 b 2 c 3) '(a 2 b 4))
(C 3 A 2 B 4)
like image 99
Rainer Joswig Avatar answered Sep 19 '22 23:09

Rainer Joswig