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

Util suggestion: plist-p #99

Open
Ambrevar opened this issue Dec 8, 2021 · 2 comments
Open

Util suggestion: plist-p #99

Ambrevar opened this issue Dec 8, 2021 · 2 comments

Comments

@Ambrevar
Copy link

Ambrevar commented Dec 8, 2021

It occurred to me a few times that I had to check whether an object is a valid plist. A plist-p helper would be nice here!

trivial-types has property-list-p but it accepts '(foo 1) which seems wrong to me.

Here is a fix:

(defun property-list-p (object)
  "XXX"
  (declare (optimize . #.*standard-optimize-qualities*))
  (typecase object
    (null t)
    (cons
     (loop
       (if (null object) 
           (return t)
           (let ((key (car object))
                 (next (cdr object)))
             (if (or (not (keywordp key))
                     (not (consp next)))
                 (return)
                 (setq object (cdr next)))))))))

Alternatively, shorter but less efficient I presume:

(defun plist-p (object)
  "Return non-nil if OBJECT is a plist."
  (and (listp object)
       (alex:proper-list-p object)
       (evenp (length object))
       (loop :for x :in object :by #'cddr
             :always (keywordp x))))

Thoughts?

@ruricolist
Copy link
Owner

I think the definition of "property list" includes any kind of symbol, not just keywords. After all you can define functions that take plain symbols as keywords (e.g. (defun fn (&key ((x x) nil)) x) which would be called as (fn 'x 1)). But I can also see the usefulness of being able to test for a property list that only allows keywords when that's what you expect.

@Ambrevar
Copy link
Author

Ambrevar commented Jan 3, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants