all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Marcin Borkowski <mbork@mbork.pl>
To: help-gnu-emacs@gnu.org
Subject: Re: alist-get in Emacs 24?
Date: Wed, 07 Oct 2015 21:15:32 +0200	[thread overview]
Message-ID: <87k2qyij75.fsf@mbork.pl> (raw)
In-Reply-To: <877fn5hgg8.fsf@web.de>


On 2015-10-02, at 09:34, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> I will probably just write functions like set-alist-element and
>> inc-alist-element (or maybe I'll name them better) so that the library
>> works in Emacs 24, but is there a better way?
>
> There was assoc.el in prior Emacsen (and it's still lying around in
> lisp/obsolete).  `aput' is its setter for alists.  Works great, but is
> obsolete.
>
> A different approach is to use `assoc', `delq' and `push', i.e., do it
> by hand.

OK, so I did it by hand (though without delq or push).  I am not
extremely happy with my solution, though, since in the setter function
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?

--8<---------------cut here---------------start------------->8---
(require 'cl)

(defun my-alist-get (key alist)
  "Return the value associated to KEY in ALIST.
This function is needed for Emacsen older than v25."
  (cdr (assoc key alist)))

(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))))))

(defun my-inc-alist-value (key alist increment)
  "Increment the value corresponding to KEY in ALIST by INCREMENT.
Throw an error if KEY is not in ALIST."
  (let ((pair (assoc key alist)))
    (if pair
	(incf (cdr pair) increment)
      (error "Nothing to increment"))))
--8<---------------cut here---------------end--------------->8---

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



  parent reply	other threads:[~2015-10-07 19:15 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 [this message]
2015-10-07 20:45     ` Drew Adams
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

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

  git send-email \
    --in-reply-to=87k2qyij75.fsf@mbork.pl \
    --to=mbork@mbork.pl \
    --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.