help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to delete all nil properties from a plist?


From: Marcin Borkowski
Subject: Re: How to delete all nil properties from a plist?
Date: Sun, 02 Aug 2015 00:11:00 +0200

On 2015-08-02, at 00:03, Marcin Borkowski <address@hidden> wrote:

> Hi all,
>
> so I'm still using plists, though I'm less and less sure that they are
> actually better than alists for my use-case.  Now I need to delete all
> properties whose value is nil.  I'm using this function:
>
> --8<---------------cut here---------------start------------->8---
> (defun plist-clear (plist)
>   "Return PLIST with all nil properties deleted."
>   (cond
>    ((< (length plist) 2) nil)
>    ((null (cadr plist)) (cddr plist))
>    (t (cons (car plist) (cons (cadr plist) (plist-clear (cddr plist)))))))
> --8<---------------cut here---------------end--------------->8---
>
> Question 1: is there a better way to write it?  (Especially the last
> line.)
>
> Question 2: how would I do the analogous thing with alists?

OK, so what about this?  Is there a better way (which would probably
mean, is such function already defined)?

--8<---------------cut here---------------start------------->8---
(defun alist-clear (alist)
  "Return ALIST with all pairs with nil value deleted."
  (if alist
      (if (cdar alist)
          (cons (car alist)
                (alist-clear (cdr alist)))
        (alist-clear (cdr alist)))))
--8<---------------cut here---------------end--------------->8---

TIA,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



reply via email to

[Prev in Thread] Current Thread [Next in Thread]