Created
November 24, 2011 05:55
-
-
Save jacius/1390715 to your computer and use it in GitHub Desktop.
merge-plists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; I'm sure this could be improved a lot! I'm still a lisp newbie. | |
(defun merge-plists (&rest plists) | |
"Merge all the given plists into a new plist. The new plist has all | |
the keys from each plist, with values of keys in later lists | |
overriding the values of the same keys in earlier plists. | |
No particular order of key/value pairs is guaranteed. | |
E.g.: | |
> (afw/util:merge-plists '(:a 1 :b 2) '(:a 3 :c 4) '(:d 5)) | |
(:D 5 :C 4 :A 3 :B 2)" | |
(let ((result (copy-list (first plists)))) | |
(dolist (plist (rest plists)) | |
(do* ((prop (first plist) (first plist)) | |
(value (second plist) (second plist)) | |
(oldpl plist plist) | |
(plist plist (cddr plist))) | |
((not oldpl)) | |
(setf (getf result prop) value))) | |
result)) | |
;; A more clear and concise version, using loop instead of do*. | |
(defun merge-plists2 (&rest plists) | |
(let ((result (copy-list (first plists)))) | |
(dolist (plist (rest plists)) | |
(loop for (key value) on plist by #'cddr | |
do (setf (getf result key) value))) | |
result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment