[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: design ponderings: plist to alist
From: |
Pascal J. Bourguignon |
Subject: |
Re: design ponderings: plist to alist |
Date: |
Wed, 16 Apr 2014 12:04:06 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Thien-Thi Nguyen <ttn@gnu.org> writes:
> This fragment (from gnugo.el):
> (let ((root (gnugo--root-node)))
> (cl-flet
> ((r! (&rest plist)
> (gnugo--decorate
> root (loop ; hmm, available elsewhere?
> while plist
> collect (let* ((k (pop plist))
> (v (pop plist)))
> (cons k v))))))
> (r! :SZ board-size
> :DT (format-time-string "%Y-%m-%d")
> :RU (if (string-match "--chinese-rules" args)
> "Chinese"
> "Japanese")
> :AP (cons "gnugo.el" gnugo-version)
> :KM komi)
> (let ((gb (gnugo--blackp (gnugo-other user-color))))
> (r! (if gb :PW :PB) (user-full-name)
> (if gb :PB :PW) (concat "GNU Go " (gnugo-query "version"))))
> (unless (zerop handicap)
> (r! :HA handicap
> :AB (mapcar (gnugo--as-cc-func)
> (gnugo-lsquery "fixed_handicap %d"
> handicap))))))
> includes a function to convert plist (succinct to humans) to alist
> (succinct to computers).
Actually, an a-list contains the same number of cons cells and requires
the same number of accesses as a p-list. I wouldn't say it's more
succinct to computer than p-list.
There are only two places where lisp marks a preference to p-list rather
than a-list:
- symbol-plist, get, (setf get)
- &key parameter.
The only advantage of A-list, is that you can process entries
independently. So if you have to move entries from place to place
or if you need to share them amongst several collections, it's
preferable to p-list where you would have to copy all the cons cells:
(let* ((a1 (list (cons :a 1) (cons :b 2) (cons :c 3)))
(a2 (list (first a1) (cons :b 3) (third a1))))
(setf (cdr (assoc :a a1)) 42)
a2)
--> ((:a . 42) (:b . 3) (:c . 3))
This wouldn't be possible with p-list, since the cons cell containing 1
couldn't have two cdr cells, to (cdr a1) and to (cdr a2).
But if you don't need to extract, insert or mutate entries
_independently_, then p-list may be prefered since they can be used with
&key.
> I spent 20 minutes poking around the Emacs
> source searching for something builtin, to no avail. I saw a few cases
> of the opposite direction (alist to plist) and many cases where plists
> are walked at time of use (e.g., the C code for text-properties), so
> maybe this is a hint that plist to alist (pre-use) is a net lose. :-/
> What do people think?
This is not a popularity contest.
--
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ? C'est le moment d'acheter !"