Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Removed recursion and a few tweaks #2

Merged
merged 3 commits into from

2 participants

@spacebat

Hi Nic,

I was playing with kv this afternoon after you reminded me of its existence. There's a nice idiom for stepping through a plist with loop on/by 'cddr. There wasn't a test for kvplist->alist so I added one and in testing I found that keyword->symbol could also act on a non-keyword so I put a check and a test in for that too.

Do what thou wilt and all that!

@nicferrier nicferrier merged commit 608de1e into nicferrier:master
@nicferrier
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2012
  1. @spacebat

    added test for kvplist->alist

    spacebat authored
  2. @spacebat

    made keyword->symbol tolerant of non-keyword symbols

    spacebat authored
    added test for keyword->symbol
  3. @spacebat
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 6 deletions.
  1. +24 −1 kv-tests.el
  2. +5 −5 kv.el
View
25 kv-tests.el
@@ -141,12 +141,35 @@
(dotassq 'a.b.c '((a . ((b . ((c . 10)))))))
10)))
+(ert-deftest keyword->symbol ()
+ "Convert keyword into a symbol without the leading `:'"
+ (should
+ (eq
+ 'key
+ (keyword->symbol :key)))
+ (should
+ (eq
+ 'key
+ (keyword->symbol 'key)))
+ (let ((sym (gensym)))
+ (should
+ (eq
+ sym
+ (keyword->symbol sym)))))
+
(ert-deftest kvalist->plist ()
"Make alists into plists."
(should
(equal
'(:a1 value1 :a2 value2)
- (kvalist->plist '((a1 . value1)(a2 . value2))))))
+ (kvalist->plist '((a1 . value1) (a2 . value2))))))
+
+(ert-deftest kvplist->alist ()
+ "Make plists into alists."
+ (should
+ (equal
+ '((a1 . value1) (a2 . value2))
+ (kvplist->alist '(:a1 value1 :a2 value2)))))
(ert-deftest kvplist->filter-keys ()
(should
View
10 kv.el
@@ -168,18 +168,18 @@ expression is true."
"A keyword is a symbol leading with a :.
Converting to a symbol means dropping the :."
- (intern (substring (symbol-name keyword) 1)))
+ (if (keywordp keyword)
+ (intern (substring (symbol-name keyword) 1))
+ keyword))
(defun kvplist->alist (plist)
"Convert PLIST to an alist.
The keys are expected to be :prefixed and the colons are removed.
The keys in the resulting alist are symbols."
- ;; RECURSION KLAXON
(when plist
- (destructuring-bind (key value &rest plist) plist
- (cons `(,(keyword->symbol key) . ,value)
- (kvplist->alist plist)))))
+ (loop for (key value . rest) on plist by 'cddr
+ collect (cons (keyword->symbol key) value))))
(defun kvalist2->plist (alist2)
"Convert a list of alists too a list of plists."
Something went wrong with that request. Please try again.