summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNic Ferrier <nic@ferrier.me.uk>2013-02-01 03:20:07 -0800
committerNic Ferrier <nic@ferrier.me.uk>2013-02-01 03:20:07 -0800
commit862be3141771c02ea0162f5917d0196d0ed4e010 (patch)
tree505d89a11a95f1b2572f964cbe69d9fe6d301aa9
parent608de1e608ba6f1a77d3ffff93dc0fea0fad3fea (diff)
parent0eede1512e5ab1c352c0dbb8610f6f27b9e8ba33 (diff)
Merge pull request #3 from leoc/master
Add kvplist-merge function - All looks good! merging and closing! I'll release a new version tonight if I can.
-rw-r--r--README.creole6
-rw-r--r--kv-tests.el15
-rw-r--r--kv.el11
3 files changed, 29 insertions, 3 deletions
diff --git a/README.creole b/README.creole
index b4a58ae..1dfd1aa 100644
--- a/README.creole
+++ b/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//.
-
-
diff --git a/kv-tests.el b/kv-tests.el
index 4a4823f..adbaa49 100644
--- a/kv-tests.el
+++ b/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
diff --git a/kv.el b/kv.el
index ffed5b6..2139b28 100644
--- a/kv.el
+++ b/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)