unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: Marcin Borkowski <mbork@mbork.pl>, help-gnu-emacs@gnu.org
Subject: RE: alist-get in Emacs 24?
Date: Wed, 7 Oct 2015 13:45:11 -0700 (PDT)	[thread overview]
Message-ID: <5830aeae-99de-461a-9414-e746edba9211@default> (raw)
In-Reply-To: <87k2qyij75.fsf@mbork.pl>

> I have to pass the alist variable name as a symbol.  (This is needed
> because I might want to use it to introduce a new key-value pair
> into the list.)  Does anyone have any suggestion for enhancing my
> solution?
> 
> (defun my-set-alist-value (key alist value)
>   "Set the value corresponding to KEY in ALIST to VALUE.
> Note: ALIST should be a symbol.  This is morally equivalent to
> `(setf (alist-get key (symbol-value alist)) value)',
> but works in older Emacsen."
>   (let ((pair (assoc key (symbol-value alist))))
>     (if pair
> 	(setcdr pair value)
>       (set alist (acons key value (symbol-value alist))))))

You say that ALIST must be a symbol here, but I don't see why.
Why not just modify the alist structure (i.e., the alist) itself?
Why repoint the variable to the result inside the set function?

If you modify the structure in place, and you return the new alist,
then you can always do this to ensure that the variable is up-to-date:
(setq THE-VAR (my-set-alist-value key alist value))

That's typically the way destructive operations are done.  It is
why you need to do (setq foo (delete 42 foo)).



  reply	other threads:[~2015-10-07 20:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-02  6:32 alist-get in Emacs 24? Marcin Borkowski
2015-10-02  6:39 ` Dmitry Gutov
2015-10-02  6:54   ` Marcin Borkowski
2015-10-02  7:34 ` Michael Heerdegen
2015-10-02  8:49   ` Marcin Borkowski
2015-10-03  3:05     ` Michael Heerdegen
2015-10-02 13:37   ` Stefan Monnier
2015-10-07 19:15   ` Marcin Borkowski
2015-10-07 20:45     ` Drew Adams [this message]
2015-10-08  8:21     ` Michael Heerdegen
2015-10-08 15:53       ` Drew Adams
2015-10-08 16:14         ` Michael Heerdegen

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5830aeae-99de-461a-9414-e746edba9211@default \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@mbork.pl \
    /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.
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).