Permalink
Browse files

Reimplement kvplist-merge as adviced by spacebat

Spacebat in the #emacs channel suggested to use the loop macro.
https://gist.github.com/4595042
  • Loading branch information...
1 parent f6e0b5a commit 0eede1512e5ab1c352c0dbb8610f6f27b9e8ba33 @leoc leoc committed Jan 22, 2013
Showing with 19 additions and 13 deletions.
  1. +3 −2 README.creole
  2. +8 −0 kv-tests.el
  3. +8 −11 kv.el
View
5 README.creole
@@ -145,9 +145,10 @@ Filter the plist to just those matching //keys//.
[[kvalist->filter-keys]] is actually used to do this work.
-=== kvplist->merge old-plist new-plist ===
+=== kvplist->merge &rest plists ===
-Merge the keys from new-plist into old-plist and return the new plist.
+Merge the 2nd and subsequent plists into the first, clobbering values set
+by lists to the left.
=== kvplist2->filter-keys plist2 &rest keys ===
View
8 kv-tests.el
@@ -186,4 +186,12 @@
(kvplist-merge '(:key1 "value1" :key2 "old value")
'(:key2 "new value" :key3 "entirely new")))))
+(ert-deftest kvplist-merge-multiple ()
+ (should
+ (equal
+ '(:key1 "value1" :key2 "new value" :key3 "overwritten new one" :key4 "second entirely new")
+ (kvplist-merge '(:key1 "value1" :key2 "old value")
+ '(:key2 "new value" :key3 "entirely new")
+ '(:key3 "overwritten new one" :key4 "second entirely new")))))
+
;;; kv-tests.el ends here
View
19 kv.el
@@ -385,17 +385,14 @@ SEXP will describe the structure desired."
(defalias 'map-bind 'kvmap-bind)
-(defun kvplist-merge (old new)
- "Merges two plists. The keys from NEW will overwrite the ones in OLD."
- (let ((key (car new))
- (val (cadr new))
- (new (cddr new)))
- (while (and key val)
- (setq old (plist-put old key val))
- (setq key (car new))
- (setq val (cadr new))
- (setq new (cddr new)))
- old))
+(defun kvplist-merge (&rest plists)
+ "Merge the 2nd and subsequent plists into the first, clobbering values set by lists to the left."
+ (let ((result (car plists))
+ (plists (cdr plists)))
+ (loop for plist in plists do
+ (loop for (key val) on plist by 'cddr do
+ (setq result (plist-put result key val))))
+ result))
(provide 'kv)
(provide 'dotassoc)

0 comments on commit 0eede15

Please sign in to comment.