From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eshel Yaron via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#75170: add-to-alist: new function Date: Sun, 29 Dec 2024 08:33:06 +0100 Message-ID: References: <878qrzm4sb.fsf@gnu.org> Reply-To: Eshel Yaron Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38108"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: 75170@debbugs.gnu.org To: Roland Winkler Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Dec 29 08:34:31 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tRnok-0009kl-V8 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 29 Dec 2024 08:34:31 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tRnoV-00049s-DQ; Sun, 29 Dec 2024 02:34:15 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tRnoJ-0003sQ-BA for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:34:05 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tRnoJ-0003no-2V for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:34:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=2wKp2Isrmk3RkTXDixhdi0Rp/TbLh7UbXr7xXXIEmqI=; b=bS55u83/AFtj4kz22hKU1K/OhN8Z+LJ8kMGL/KxRqtNLWH7sZ12w82fdbtKRLPgpaGjZ6/0w7B7krQnJLcrqf4LfM0ejYXxO3T5p8OyjYnQSMiRSDxw5qRkaPc8ooJZ3NosIZopg8YzKoxSM4avp7PYabV4BwjKgapODQ42N3GWt4fGskaMDubidRWDNeXN0wudBOTaGQWZJt95VXdEq+gvcl7BIz8cJW7yEg/pxV3UKB2YflDWI45scWRRb13uX6bV8CYIHhBWbWSdP/Vg/Ml7sJk/SwmuJBo9OWDzRvXSaLlDjnskZ44PCHaXoI5wol0YAuGvVjKVP6sJieJVDlg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tRnoI-00085o-Hx for bug-gnu-emacs@gnu.org; Sun, 29 Dec 2024 02:34:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eshel Yaron Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 29 Dec 2024 07:34:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 75170 X-GNU-PR-Package: emacs Original-Received: via spool by 75170-submit@debbugs.gnu.org id=B75170.173545759131028 (code B ref 75170); Sun, 29 Dec 2024 07:34:02 +0000 Original-Received: (at 75170) by debbugs.gnu.org; 29 Dec 2024 07:33:11 +0000 Original-Received: from localhost ([127.0.0.1]:53617 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRnnS-00084N-N5 for submit@debbugs.gnu.org; Sun, 29 Dec 2024 02:33:11 -0500 Original-Received: from mail.eshelyaron.com ([107.175.124.16]:55818 helo=eshelyaron.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tRnnQ-000848-Gq for 75170@debbugs.gnu.org; Sun, 29 Dec 2024 02:33:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=eshelyaron.com; s=mail; t=1735457588; bh=MJRKQcu3KampE4f/hQoHEtx6jLtAoIWotqV7LJYRZt4=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=PEaBYoayLVsogaq7+ylxDM/Ao0kDIl0uyxbKZlkN5BvlrCdYs4StO2zsYPrdazPUx ksa4Mnn2TXsKblu8FdTTLNUFSGvjiVh3N/QSvIY8QSF9VQHoLXk8mqNkNyIXCZnXIl /BigwDNyW9Pn9KFKr+6fsMxJ0XgDhgKiTy3lz6/kZFOH4XI804V7vG1jxLVZ0Ik6Y6 2lRP8UnPDEZyJ+/B4rJlpnGicrRJReDAWHDspV874t4o6/+PmqhmFVh678zgi4cLyT ao8LNQDMhV4n16sIfQWeFKIa5RFt1xD+G6g70JXDzWQQOUJD5Z8U/bVt4wDsrY4USI qekjUh3PNro3g== In-Reply-To: <878qrzm4sb.fsf@gnu.org> (Roland Winkler's message of "Sat, 28 Dec 2024 23:33:56 -0600") X-Hashcash: 1:20:241229:winkler@gnu.org::Kf76vHcV8XLKx52l:1k5s X-Hashcash: 1:20:241229:75170@debbugs.gnu.org::rw+OTBiUW4G9jZkI:2VHG X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:297935 Archived-At: Hello, Roland Winkler writes: > On Mon, 12 Feb 2001, Stephen Gildea wrote: [...] > While I thought about a function add-to-alist I found the above thread > from 24 years ago. Stephen's message describes nicely when such a > function can be useful. I suggest to add such a function to subr.el. > > The code below follows the conventions of add-to-list. > > (defun add-to-alist (alist-var elt-cons &optional no-replace append compare-fn) > "Add ELT-CONS to the value of ALIST-VAR if it isn't there yet. > If an element with the same car as the car of ELT-CONS is already present > in ALIST-VAR, replace it with ELT-CONS unless NO-REPLACE is non-nil. > If a matching element is not yet present, add ELT-CONS at the beginning > of ALIST-VAR. If APPEND is non-nil, add ELT-CONS at the end of ALIST-VAR. > The test for presence of ELT-CONS is done with `equal', or with COMPARE-FN > if that's non-nil. > ALIST-VAR should not refer to a lexical variable. > > The return value is the new value of ALIST-VAR." > (let ((elt (cond ((or (null compare-fn) (eq compare-fn #'equal)) > (assoc (car elt-cons) (symbol-value alist-var))) > ((eq compare-fn #'eq) > (assq (car elt-cons) (symbol-value alist-var))) > (t > (let ((alist (symbol-value alist-var)) > (key (car elt-cons))) > (while (and alist > (not (funcall compare-fn key (caar alist)))) > (setq alist (cdr alist))) > (car alist)))))) > (if elt > (progn > (unless no-replace > (setcdr elt (cdr elt-cons))) > (symbol-value alist-var)) > (set alist-var > (if append > (append (symbol-value alist-var) (list elt-cons)) > (cons elt-cons (symbol-value alist-var))))))) FWIW, in my working branch I use alist-set which does something similar, I think: --8<---------------cut here---------------start------------->8--- (defun alist-set (key alist value &optional testfn) "Associate VALUE with KEY in ALIST, comparing keys with TESTFN." (setf (alist-get key alist nil nil testfn) value)) --8<---------------cut here---------------end--------------->8--- Cheers, Eshel