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: John Mastro
Subject: Re: How to delete all nil properties from a plist?
Date: Wed, 5 Aug 2015 18:05:17 -0700

> This only uses `cons', `cdr', and `nreverse', so it is
> linear, right? ("linear" only from going thru the
> input list.)
>
> (require 'cl)
>
> (defun plist-drop-nil-props (l)
>   (let ((new)
>         (nil-prop)
>         (prop) )
>     (cl-loop for x in l
>              do (if (setq prop (not prop))
>                     (if x (setq new (cons x new))
>                       (setq nil-prop t))
>                   (if x
>                       (if nil-prop (setq nil-prop nil)
>                         (setq new (cons x new))   )
>                     (setq new (cdr new) ))))
>     (nreverse new) ))

I took a swing at this too for the fun of it. Here's what I ended
up with:

    (defun plist-drop-nil-props (plist)
      (let* ((result (list 'head))
             (last result))
        (cl-loop for (key val) on plist by #'cddr do
                 (when-let (new (and val (list key val)))
                   (setcdr last new)
                   (setq last (cdr new))))
        (cdr result)))

The `when-let' macro is new in Emacs 25 but obviously it can be
trivially replaced with `let' and `when' (or `-when-let' from
dash.el).

-- 
john



reply via email to

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