all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: design ponderings: plist to alist
Date: Wed, 16 Apr 2014 12:04:06 +0200	[thread overview]
Message-ID: <871twx3815.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: 87a9bl7rkd.fsf@zigzag.favinet

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 !"




  parent reply	other threads:[~2014-04-16 10:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-16  5:48 design ponderings: plist to alist Thien-Thi Nguyen
2014-04-16  8:16 ` Thorsten Jolitz
2014-04-16  8:35   ` Thien-Thi Nguyen
2014-04-16 10:04 ` Pascal J. Bourguignon [this message]
2014-04-16 17:20   ` Stefan Monnier
2014-04-16 17:29 ` Stefan Monnier

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=871twx3815.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.