From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: How to iterate over properties in a plist? Date: Sat, 01 Aug 2015 01:33:23 +0200 Organization: Informatimago Message-ID: <873803x5q4.fsf@kuiper.lan.informatimago.com> References: <876150vwaa.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1438385726 12842 80.91.229.3 (31 Jul 2015 23:35:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 31 Jul 2015 23:35:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Aug 01 01:35:20 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZLJpf-0006MH-6E for geh-help-gnu-emacs@m.gmane.org; Sat, 01 Aug 2015 01:35:19 +0200 Original-Received: from localhost ([::1]:46311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZLJpe-0004MW-Gr for geh-help-gnu-emacs@m.gmane.org; Fri, 31 Jul 2015 19:35:18 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 61 Original-X-Trace: individual.net wya4+ivW/1q6P3Chd3Jqvww/qpb/B5hhON6J8kAzt17Vwubuf6 Cancel-Lock: sha1:NTUzOTYyYWYxYjc4MWQ5ZGI5YmFkYTRlZjVhZjM0ZjI2OTczMGEyMQ== sha1:YQK8wOFugpfm829RP27r2nwr+7k= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:213890 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106175 Archived-At: Marcin Borkowski writes: > On 2015-08-01, at 00:18, Stefan Monnier 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