From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: alist-get in Emacs 24? Date: Wed, 07 Oct 2015 21:15:32 +0200 Message-ID: <87k2qyij75.fsf@mbork.pl> References: <871tddvl08.fsf@mbork.pl> <877fn5hgg8.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1444245372 29917 80.91.229.3 (7 Oct 2015 19:16:12 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 7 Oct 2015 19:16:12 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Oct 07 21:16:02 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 1ZjuBz-0008Dc-JG for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Oct 2015 21:15:59 +0200 Original-Received: from localhost ([::1]:59127 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjuBz-0008TP-1Q for geh-help-gnu-emacs@m.gmane.org; Wed, 07 Oct 2015 15:15:59 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41662) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjuBo-0008TE-GN for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 15:15:49 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZjuBj-0001uM-Jd for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 15:15:48 -0400 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:50496) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjuBj-0001s7-D9 for help-gnu-emacs@gnu.org; Wed, 07 Oct 2015 15:15:43 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id 3169C8F200D for ; Wed, 7 Oct 2015 21:15:40 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id CYoFVuz83s-U for ; Wed, 7 Oct 2015 21:15:38 +0200 (CEST) Original-Received: from localhost (103-115.echostar.pl [213.156.103.115]) by mail.mojserwer.eu (Postfix) with ESMTPSA id EED958F200C for ; Wed, 7 Oct 2015 21:15:37 +0200 (CEST) In-reply-to: <877fn5hgg8.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a01:5e00:2:52::8 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:107544 Archived-At: On 2015-10-02, at 09:34, Michael Heerdegen wrote: > Marcin Borkowski 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