From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Kangas Newsgroups: gmane.emacs.bugs Subject: bug#66509: 29.1.50; let-alist should support numeric indexing Date: Tue, 10 Sep 2024 19:22:16 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8380"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Artur Malabarba , 66509@debbugs.gnu.org To: Spencer Baugh Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 11 01:24:25 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 1soADg-00020y-6a for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 11 Sep 2024 01:24:24 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1soADH-0006qe-7v; Tue, 10 Sep 2024 19:23:59 -0400 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 1soADG-0006qV-0o for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 19:23:58 -0400 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 1soADF-000536-Nx for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 19:23:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=Date:MIME-Version:References:In-Reply-To:From:To:Subject; bh=MYQOXV/ISne2HdmNJ+XvVo0wn0Vmf01l21XJr6NCGKg=; b=B6lnCslFlfV7W3ly42z2b936eB4wKgc28tvdVAeyElq1s0R8qZjigzM5pv8ToKxpoEgH69+8zgNi/sg9azeLW7or3kN9eCw1IfW1Ftm1wepcDCnLsKMlyBzkaVEGWNvGvTNjcgNR78kPuYvd5s8qS4n/ykJhH/Pxwjye2D0LDHTDtcdG3AqO7uvXV66601yrqTOcjRVFTF8q4ojVSNNAlNGRbtCb+3036e49VtKQSGUBpp6GHroQDcQrFXiq3euNymqIB9YgWFVOzxnwyIpyZysL0yDbduAgGXe+wCZ/ZatvNUA+6WTiqLELRxJRYPe/Dev+7TX/DZ3FlbOVPlyByg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1soADK-000133-7d for bug-gnu-emacs@gnu.org; Tue, 10 Sep 2024 19:24:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Kangas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 10 Sep 2024 23:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66509 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 66509-submit@debbugs.gnu.org id=B66509.17260106113986 (code B ref 66509); Tue, 10 Sep 2024 23:24:02 +0000 Original-Received: (at 66509) by debbugs.gnu.org; 10 Sep 2024 23:23:31 +0000 Original-Received: from localhost ([127.0.0.1]:37152 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1soACo-00012E-J3 for submit@debbugs.gnu.org; Tue, 10 Sep 2024 19:23:30 -0400 Original-Received: from mail-ed1-f54.google.com ([209.85.208.54]:55596) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1soACl-00011z-Q9 for 66509@debbugs.gnu.org; Tue, 10 Sep 2024 19:23:28 -0400 Original-Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-5bf01bdaff0so6870555a12.3 for <66509@debbugs.gnu.org>; Tue, 10 Sep 2024 16:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726010536; x=1726615336; darn=debbugs.gnu.org; h=cc:to:subject:message-id:date:mime-version:references:in-reply-to :from:from:to:cc:subject:date:message-id:reply-to; bh=MYQOXV/ISne2HdmNJ+XvVo0wn0Vmf01l21XJr6NCGKg=; b=cseHIY7lhgY+ueJJ+FiPDTMFv4k6KUWGLheAG8xNwqyPikq+m4SSo902NDeRnTHtP7 IEaOQ6rkGHUs0cH6MdFPELqaHsscrwxblOFy6/txpP7w0ncyhUKva5KXA+xlCvVjuQc6 v31UzxY0rLSkQC4OtQwKXMsnHtVsi/LzwJLmoYLQeTu2hkiLVYgsDqoAshUPHlAvYhxs 1qG3CD9w+DKX2Oqm907IRUVWnwzz1CdIzdiY2XnOhVVwLVDDThRCkL1YPUFykU5ear24 KAkYnXkw/IkTQT8mltgtwuZDbQHPcx5c74TGhKHBo6QEzB409dMZTM95RVWTpciTr4Z9 o1zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726010536; x=1726615336; h=cc:to:subject:message-id:date:mime-version:references:in-reply-to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MYQOXV/ISne2HdmNJ+XvVo0wn0Vmf01l21XJr6NCGKg=; b=JceQ5qfcNNEhN/8vK9/bCl+Em2sS3RsshiAfO5dVofZplqHSQbiUFL6XqEJrEFFrdB 742FDpjSluv2usjDcPr/COWLu0tkBCHF+WDAdtlUItfM5mpAlWqijd0lN21gRNzEo+3p SV7WbxskRVA+X1sRYYD1MOrVOBnL9pB0CBN4sLjX8hpX/7tpSzbjbRacN25eBH5SydmA hco78tXE9wO5W0VxyX59rhZbV+GNTh5oUgzT4hH5dlrwyjwDV7qe0ZgL/bxexncvOFYF XfaqP/1vMO5b9WNqpl0bA5a9ckkdAkKnk2t4sAa910st1aw/gwYP2X+C9KKpvAf+fMYt /r1w== X-Gm-Message-State: AOJu0YxjlN3roXSD5TI42wHsqe2oZmd+8zY0MvlpfBrMVIvUT05NLRI+ ylWSdJST5j1UIsoW+jqkX2i8PSqRSvl9adJLTyz8OUUmQJ1ODXp4AVhsIjGlSG3WCeA1JMoimCG 7Eap3E4KzDCp+jv2WysYQM1k2nHo= X-Google-Smtp-Source: AGHT+IGy/mAPonwoL/cl5nIW5LF6nctr37QMsiPaC7LABj9RJTHW89NXQ9EmvZT0fnI5E38ZTzcTyTa6NcYQUdr4rJA= X-Received: by 2002:a05:6402:13d5:b0:5c2:4dcc:b911 with SMTP id 4fb4d7f45d1cf-5c3eac20d9emr9404865a12.26.1726010536432; Tue, 10 Sep 2024 16:22:16 -0700 (PDT) Original-Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Tue, 10 Sep 2024 19:22:16 -0400 In-Reply-To: (Spencer Baugh's message of "Thu, 12 Oct 2023 18:04:00 -0400") 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:291580 Archived-At: Artur, do you have any thoughts on the below patch? Spencer Baugh writes: > From 28e52a343f72dddd991cafd23fea910cc5f64ac5 Mon Sep 17 00:00:00 2001 > From: Spencer Baugh > Date: Thu, 12 Oct 2023 18:01:46 -0400 > Subject: [PATCH] Support numeric indexing in let-alist > > let-alist is very useful. But sometimes an alist contains a list in > the middle, which contains yet more alists. Previously, this was > somewhat painful to deal with, and required something like: > > (let-alist alist > (let-alist (nth 0 .a) > (let-alist (nth 3 .b) > .c))) > > Now, the following works: > > (let-alist alist > .a.0.b.3.c) > > * lisp/emacs-lisp/let-alist.el (let-alist--access-sexp): Properly > parse numbers. > (let-alist--list-to-sexp): Use nth to handle numbers. > (let-alist): Update docs. > --- > lisp/emacs-lisp/let-alist.el | 25 +++++++++++++++---------- > 1 file changed, 15 insertions(+), 10 deletions(-) > > diff --git a/lisp/emacs-lisp/let-alist.el b/lisp/emacs-lisp/let-alist.el > index d9ad46b2af7..de7c087bf2a 100644 > --- a/lisp/emacs-lisp/let-alist.el > +++ b/lisp/emacs-lisp/let-alist.el > @@ -36,22 +36,23 @@ > ;; symbol inside body is let-bound to their cdrs in the alist. Dotted > ;; symbol is any symbol starting with a `.'. Only those present in > ;; the body are let-bound and this search is done at compile time. > +;; A number will result in a list index. > ;; > ;; For instance, the following code > ;; > ;; (let-alist alist > -;; (if (and .title .body) > +;; (if (and .title.0 .body) > ;; .body > ;; .site > ;; .site.contents)) > ;; > ;; essentially expands to > ;; > -;; (let ((.title (cdr (assq 'title alist))) > +;; (let ((.title.0 (nth 0 (cdr (assq 'title alist)))) > ;; (.body (cdr (assq 'body alist))) > ;; (.site (cdr (assq 'site alist))) > ;; (.site.contents (cdr (assq 'contents (cdr (assq 'site alist)))))) > -;; (if (and .title .body) > +;; (if (and .title.0 .body) > ;; .body > ;; .site > ;; .site.contents)) > @@ -93,14 +94,17 @@ let-alist--access-sexp > (if (string-match "\\`\\." name) > clean > (let-alist--list-to-sexp > - (mapcar #'intern (nreverse (split-string name "\\."))) > + (mapcar #'read (nreverse (split-string name "\\."))) > variable)))) > > (defun let-alist--list-to-sexp (list var) > "Turn symbols LIST into recursive calls to `cdr' `assq' on VAR." > - `(cdr (assq ',(car list) > - ,(if (cdr list) (let-alist--list-to-sexp (cdr list) var) > - var)))) > + (let ((sym (car list)) > + (rest (if (cdr list) (let-alist--list-to-sexp (cdr list) var) > + var))) > + (cond > + ((numberp sym) `(nth ,sym ,rest)) > + (t `(cdr (assq ',sym ,rest)))))) > > (defun let-alist--remove-dot (symbol) > "Return SYMBOL, sans an initial dot." > @@ -116,22 +120,23 @@ let-alist > "Let-bind dotted symbols to their cdrs in ALIST and execute BODY. > Dotted symbol is any symbol starting with a `.'. Only those present > in BODY are let-bound and this search is done at compile time. > +A number will result in a list index. > > For instance, the following code > > (let-alist alist > - (if (and .title .body) > + (if (and .title.0 .body) > .body > .site > .site.contents)) > > essentially expands to > > - (let ((.title (cdr (assq \\='title alist))) > + (let ((.title (nth 0 (cdr (assq \\='title alist)))) > (.body (cdr (assq \\='body alist))) > (.site (cdr (assq \\='site alist))) > (.site.contents (cdr (assq \\='contents (cdr (assq \\='site alist)))))) > - (if (and .title .body) > + (if (and .title.0 .body) > .body > .site > .site.contents))