From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to iterate over properties in a plist?
Date: Sat, 01 Aug 2015 01:33:23 +0200 [thread overview]
Message-ID: <873803x5q4.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.7705.1438381807.904.help-gnu-emacs@gnu.org
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
next prev parent reply other threads:[~2015-07-31 23:33 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-07-31 21:42 How to iterate over properties in a plist? 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 [this message]
2015-08-01 22:49 ` Stefan Monnier
[not found] ` <mailman.7750.1438469396.904.help-gnu-emacs@gnu.org>
2015-08-04 10:15 ` plists, alists, and hashtables (was: How to iterate over properties in a plist?) Ted Zlatanov
2015-08-04 10:29 ` Nicolas Petton
[not found] ` <mailman.7809.1438684158.904.help-gnu-emacs@gnu.org>
2015-08-04 11:23 ` plists, alists, and hashtables Ted Zlatanov
2015-08-05 4:36 ` plists, alists, and hashtables (was: How to iterate over properties in a plist?) Rusi
2015-08-05 6:12 ` plists, alists, and hashtables Pascal J. Bourguignon
2015-08-05 9:47 ` Ted Zlatanov
2015-08-05 12:20 ` Rusi
2015-08-06 19:16 ` Stefan Monnier
[not found] ` <mailman.7892.1438888819.904.help-gnu-emacs@gnu.org>
2015-08-07 16:33 ` Rusi
2015-08-05 17:24 ` Pascal J. Bourguignon
2015-08-05 18:31 ` Ted Zlatanov
2015-08-05 19:30 ` Barry Margolin
2015-08-05 19:40 ` Robert Thorpe
2015-08-05 21:11 ` Pascal J. Bourguignon
2015-08-06 15:17 ` Ted Zlatanov
2015-08-06 18:46 ` Pascal J. Bourguignon
2015-08-06 20:19 ` Drew Adams
2015-08-06 21:08 ` Ted Zlatanov
2015-08-07 0:23 ` Pascal J. Bourguignon
2015-08-06 19:35 ` Stefan Monnier
2015-08-05 13:48 ` Drew Adams
2015-08-06 19:12 ` Stefan Monnier
[not found] ` <mailman.7890.1438888393.904.help-gnu-emacs@gnu.org>
2015-08-06 20:00 ` Pascal J. Bourguignon
2015-08-06 20:57 ` Ted Zlatanov
2015-08-06 21:10 ` Drew Adams
[not found] ` <mailman.7902.1438895429.904.help-gnu-emacs@gnu.org>
2015-08-06 21:15 ` Ted Zlatanov
2015-08-06 21:31 ` Stefan Monnier
2015-08-07 1:53 ` Ted Zlatanov
2015-08-07 7:34 ` Pascal J. Bourguignon
2015-08-07 16:32 ` Stefan Monnier
[not found] ` <mailman.7941.1438965165.904.help-gnu-emacs@gnu.org>
2015-08-08 3:48 ` Pascal J. Bourguignon
2015-08-08 13:42 ` Stefan Monnier
2015-08-08 14:51 ` Rusi
2015-08-07 0:08 ` Pascal J. Bourguignon
2015-08-07 2:14 ` Ted Zlatanov
2015-08-07 7:53 ` Pascal J. Bourguignon
2015-08-07 11:21 ` Ted Zlatanov
2015-08-07 11:47 ` Pascal J. Bourguignon
2015-08-07 17:21 ` Ted Zlatanov
2015-08-07 19:21 ` Stefan Monnier
[not found] ` <mailman.7952.1438975314.904.help-gnu-emacs@gnu.org>
2015-08-08 3:52 ` Pascal J. Bourguignon
2015-08-07 16:35 ` Stefan Monnier
[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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=873803x5q4.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.