Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kvplist-merge function #3

Merged
merged 2 commits into from Feb 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.creole
Expand Up @@ -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//.


15 changes: 15 additions & 0 deletions kv-tests.el
Expand Up @@ -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
11 changes: 10 additions & 1 deletion kv.el
Expand Up @@ -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."
Expand Down Expand Up @@ -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)

Expand Down