unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to iterate over properties in a plist?
@ 2015-07-31 21:42 Marcin Borkowski
  2015-07-31 22:18 ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Marcin Borkowski @ 2015-07-31 21:42 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I need to iterate over all properties in a plist.  Obviously, mapcar (or
mapc, since I need side effects only) is of no use for me.  I can easily
write a plist-mapc function:

--8<---------------cut here---------------start------------->8---
(defun plist-mapc (function plist)
  "Iterate FUNCTION (a two-argument function) over PLIST.  Error
checking is for weenies."
  (when plist
    (funcall function (car plist) (cadr plist))
    (plist-mapc function (cddr plist))))
--8<---------------cut here---------------end--------------->8---

but maybe it's there already?

TIA,

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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
  2015-07-31 21:42 Marcin Borkowski
@ 2015-07-31 22:18 ` Stefan Monnier
  2015-07-31 22:29   ` Marcin Borkowski
       [not found]   ` <mailman.7705.1438381807.904.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Stefan Monnier @ 2015-07-31 22:18 UTC (permalink / raw)
  To: help-gnu-emacs

> I need to iterate over all properties in a plist.

First things first: go complain to whoever decided to use a plist
instead of an alist.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
  2015-07-31 22:18 ` Stefan Monnier
@ 2015-07-31 22:29   ` Marcin Borkowski
  2015-07-31 22:42     ` Dmitry Gutov
       [not found]   ` <mailman.7705.1438381807.904.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 8+ messages in thread
From: Marcin Borkowski @ 2015-07-31 22:29 UTC (permalink / raw)
  To: help-gnu-emacs


On 2015-08-01, at 00:18, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> I need to iterate over all properties in a plist.
>
> First things first: go complain to whoever decided to use a plist
> instead of an alist.

Why?  I did consider both and decided that a plist will be better in my
use-case.  Reasons: it is short anyway (no more than 3-5 properties at
most), and I need to change it frequently (i.e., change the values of
individual properties).  This last operation seems much nicer in
a plist.  AFAIK, the "canonical" way to change a key-value pair in an
alist is to push the new one at the beginning.  In my case, the list
will grow quickly.

>         Stefan

Best,

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



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
  2015-07-31 22:29   ` Marcin Borkowski
@ 2015-07-31 22:42     ` Dmitry Gutov
  2015-08-01 13:34       ` Michael Heerdegen
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry Gutov @ 2015-07-31 22:42 UTC (permalink / raw)
  To: Marcin Borkowski, help-gnu-emacs

On 08/01/2015 01:29 AM, Marcin Borkowski wrote:

> AFAIK, the "canonical" way to change a key-value pair in an
> alist is to push the new one at the beginning.  In my case, the list
> will grow quickly.

(setcdr (assoc value alist) new-value) works pretty well (but you'll 
probably need to add a not-found check).



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
       [not found]   ` <mailman.7705.1438381807.904.help-gnu-emacs@gnu.org>
@ 2015-07-31 23:33     ` Pascal J. Bourguignon
  2015-08-01 22:49       ` Stefan Monnier
  0 siblings, 1 reply; 8+ messages in thread
From: Pascal J. Bourguignon @ 2015-07-31 23:33 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> On 2015-08-01, at 00:18, Stefan Monnier <monnier@iro.umontreal.ca> wrote:
>
>>> I need to iterate over all properties in a plist.
>>
>> First things first: go complain to whoever decided to use a plist
>> instead of an alist.
>
> Why?  I did consider both and decided that a plist will be better in my
> use-case.  Reasons: it is short anyway (no more than 3-5 properties at
> most), and I need to change it frequently (i.e., change the values of
> individual properties).  This last operation seems much nicer in
> a plist.  AFAIK, the "canonical" way to change a key-value pair in an
> alist is to push the new one at the beginning.  In my case, the list
> will grow quickly.

You can use mutation on a-lists, and you can push new values on p-lists
too.

There's no difference between a-lists and p-list:
- in both cases, you need to traverse two cons to check the next key.
- you have the same number of memory accesses for all the operations.

It's really only a matter of taste.

Plus, p-list can be used to pass &key arguments to functions.

(defun* f (&key a b c)
  (list a b c))

(apply (function f) '(:a 1 :c 2))
--> (1 nil 2)

and also with destructuring-bind:

   (destructuring-bind (&key a b c) '(:a 1 :c 2)
      (list a b c))
   --> (1 nil 2)

You cannot do that with a-lists, there's no way to define an argument
taking and destructuring an a-list cons cell.

So if you ever have to use the contents of your dictionary as flat arguments
to a function, you will prefer a p-list.

(But of course, you can also write your function as taking a single
a-list or a single p-list, and query the parameter instead of
destructuring it into separate parameters).


Otherwise getf use eql to compare keys, &key accepts only symbols, while
assoc* (assoc in Common Lisp), takes a :test and a :key argument to find
keys.  So if you use those standard lisp functions to process a-lists
and p-list, this may further constraint your choice.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
       [not found] <mailman.7702.1438378982.904.help-gnu-emacs@gnu.org>
@ 2015-07-31 23:34 ` Pascal J. Bourguignon
  0 siblings, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2015-07-31 23:34 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> Hi all,
>
> I need to iterate over all properties in a plist.  Obviously, mapcar (or
> mapc, since I need side effects only) is of no use for me.  I can easily
> write a plist-mapc function:
>
> (defun plist-mapc (function plist)
>   "Iterate FUNCTION (a two-argument function) over PLIST.  Error
> checking is for weenies."
>   (when plist
>     (funcall function (car plist) (cadr plist))
>     (plist-mapc function (cddr plist))))
>
> but maybe it's there already?

There's:

    (loop for (k v) on p-list by (function cddr)
          do (something-with :key k :value v))

But you might still want to abstrat it away in a map-plist function.

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
  2015-07-31 22:42     ` Dmitry Gutov
@ 2015-08-01 13:34       ` Michael Heerdegen
  0 siblings, 0 replies; 8+ messages in thread
From: Michael Heerdegen @ 2015-08-01 13:34 UTC (permalink / raw)
  To: help-gnu-emacs

Dmitry Gutov <dgutov@yandex.ru> writes:

> (setcdr (assoc value alist) new-value) works pretty well (but you'll
> probably need to add a not-found check).

And in Emacs 25:

  (setf (alist-get key alist) new-value)

even without not-found checking.


Michael.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to iterate over properties in a plist?
  2015-07-31 23:33     ` Pascal J. Bourguignon
@ 2015-08-01 22:49       ` Stefan Monnier
  0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2015-08-01 22:49 UTC (permalink / raw)
  To: help-gnu-emacs

> There's no difference between a-lists and p-list:

Far from it, there are many reasons to prefer alists:

- there's twice as much memory parallelism in alists.
- plists need to be "parsed" in order to figure out if an element is a key
  or a value.
- plists come much later in the dictionary.


        Stefan




^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-08-01 22:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.7702.1438378982.904.help-gnu-emacs@gnu.org>
2015-07-31 23:34 ` How to iterate over properties in a plist? Pascal J. Bourguignon
2015-07-31 21:42 Marcin Borkowski
2015-07-31 22:18 ` Stefan Monnier
2015-07-31 22:29   ` Marcin Borkowski
2015-07-31 22:42     ` Dmitry Gutov
2015-08-01 13:34       ` Michael Heerdegen
     [not found]   ` <mailman.7705.1438381807.904.help-gnu-emacs@gnu.org>
2015-07-31 23:33     ` Pascal J. Bourguignon
2015-08-01 22:49       ` Stefan Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).