Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add kvplist-merge function #3

Merged
merged 2 commits into from

2 participants

@leoc

I stumbled upon this situation now for the second time. I needed to update a defaults plist with some other new values from a plist that I got as function parameter. In the #emacs channel spacebat pointed me to your lib so I wrote a little function.

@leoc

I now use the gist from spacebat.

@nicferrier nicferrier merged commit 862be31 into from
@nicferrier
Owner

new version on marmalade includes it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 22, 2013
  1. @leoc
  2. @leoc

    Reimplement kvplist-merge as adviced by spacebat

    leoc authored
    Spacebat in the #emacs channel suggested to use the loop macro.
    https://gist.github.com/4595042
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 3 deletions.
  1. +4 −2 README.creole
  2. +15 −0 kv-tests.el
  3. +10 −1 kv.el
View
6 README.creole
@@ -145,9 +145,11 @@ Filter the plist to just those matching //keys//.
[[kvalist->filter-keys]] is actually used to do this work.
+=== kvplist->merge &rest plists ===
+
+Merge the 2nd and subsequent plists into the first, clobbering values set
+by lists to the left.
=== kvplist2->filter-keys plist2 &rest keys ===
Return the //plist2// (a list of plists) filtered to the //keys//.
-
-
View
15 kv-tests.el
@@ -179,4 +179,19 @@
(list :key1 "value1" :key2 t :key3 '(big list of symbols) :key4 10)
'key1 'key4))))
+(ert-deftest kvplist-merge ()
+ (should
+ (equal
+ '(:key1 "value1" :key2 "new value" :key3 "entirely new")
+ (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
11 kv.el
@@ -179,7 +179,7 @@ The keys are expected to be :prefixed and the colons are removed.
The keys in the resulting alist are symbols."
(when plist
(loop for (key value . rest) on plist by 'cddr
- collect (cons (keyword->symbol key) value))))
+ collect (cons (keyword->symbol key) value))))
(defun kvalist2->plist (alist2)
"Convert a list of alists too a list of plists."
@@ -385,6 +385,15 @@ SEXP will describe the structure desired."
(defalias 'map-bind 'kvmap-bind)
+(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)
Something went wrong with that request. Please try again.