GNU bug report logs - #66509
29.1.50; let-alist should support numeric indexing

Previous Next

Package: emacs;

Reported by: Spencer Baugh <sbaugh <at> janestreet.com>

Date: Thu, 12 Oct 2023 22:04:01 UTC

Severity: wishlist

Tags: patch

Found in version 29.1.50

Done: Eli Zaretskii <eliz <at> gnu.org>

To reply to this bug, email your comments to 66509 AT debbugs.gnu.org.
There is no need to reopen the bug first.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Thu, 12 Oct 2023 22:04:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Spencer Baugh <sbaugh <at> janestreet.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Thu, 12 Oct 2023 22:04:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.1.50; let-alist should support numeric indexing
Date: Thu, 12 Oct 2023 18:03:22 -0400
It would be nice if let-alist supported indexing.  So if my alist
contained lists in some places, instead of writing:

(let-alist alist
  (let-alist (nth 0 .a)
    (let-alist (nth 3 .b)
       .c)))

I could instead write:

(let-alist alist
  .a.0.b.3.c)

A patch which does this to follow.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Thu, 12 Oct 2023 22:05:02 GMT) Full text and rfc822 format available.

Message #8 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Thu, 12 Oct 2023 18:04:00 -0400
[0001-Support-numeric-indexing-in-let-alist.patch (text/x-patch, inline)]
From 28e52a343f72dddd991cafd23fea910cc5f64ac5 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> janestreet.com>
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))
-- 
2.39.3





Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefankangas <at> gmail.com> to control <at> debbugs.gnu.org. (Sun, 22 Oct 2023 19:49:04 GMT) Full text and rfc822 format available.

Added tag(s) patch. Request was from Spencer Baugh <sbaugh <at> janestreet.com> to control <at> debbugs.gnu.org. (Fri, 16 Feb 2024 22:42:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Tue, 10 Sep 2024 23:24:02 GMT) Full text and rfc822 format available.

Message #15 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Spencer Baugh <sbaugh <at> janestreet.com>
Cc: Artur Malabarba <arturmalabarba <at> gmail.com>, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Tue, 10 Sep 2024 19:22:16 -0400
Artur, do you have any thoughts on the below patch?

Spencer Baugh <sbaugh <at> janestreet.com> writes:

> From 28e52a343f72dddd991cafd23fea910cc5f64ac5 Mon Sep 17 00:00:00 2001
> From: Spencer Baugh <sbaugh <at> janestreet.com>
> 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))




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Tue, 17 Sep 2024 02:10:01 GMT) Full text and rfc822 format available.

Message #18 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Artur Malabarba <arturmalabarba <at> gmail.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: Spencer Baugh <sbaugh <at> janestreet.com>, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Mon, 16 Sep 2024 23:07:33 -0300
[Message part 1 (text/plain, inline)]
Hi all,

The patch looks good.
Stefan, would you mind applying it? (It'll take a while for me to get
everything configured.)

best,
Artur


On Tue, 10 Sept 2024 at 20:22, Stefan Kangas <stefankangas <at> gmail.com> wrote:

> Artur, do you have any thoughts on the below patch?
>
> Spencer Baugh <sbaugh <at> janestreet.com> writes:
>
> > From 28e52a343f72dddd991cafd23fea910cc5f64ac5 Mon Sep 17 00:00:00 2001
> > From: Spencer Baugh <sbaugh <at> janestreet.com>
> > 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))
>
[Message part 2 (text/html, inline)]

Reply sent to Stefan Kangas <stefankangas <at> gmail.com>:
You have taken responsibility. (Thu, 19 Sep 2024 01:59:02 GMT) Full text and rfc822 format available.

Notification sent to Spencer Baugh <sbaugh <at> janestreet.com>:
bug acknowledged by developer. (Thu, 19 Sep 2024 01:59:02 GMT) Full text and rfc822 format available.

Message #23 received at 66509-done <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Artur Malabarba <arturmalabarba <at> gmail.com>
Cc: Spencer Baugh <sbaugh <at> janestreet.com>, 66509-done <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Wed, 18 Sep 2024 18:56:37 -0700
Version: 31.1

Artur Malabarba <arturmalabarba <at> gmail.com> writes:

> Hi all,
>
> The patch looks good.

Thanks.

> Stefan, would you mind applying it? (It'll take a while for me to get
> everything configured.)

Pushed to master (commit ae4171efdc6).

I'm therefore closing this bug report.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Thu, 19 Sep 2024 05:58:02 GMT) Full text and rfc822 format available.

Message #26 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: sbaugh <at> janestreet.com, stefankangas <at> gmail.com, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Thu, 19 Sep 2024 08:56:52 +0300
> Resent-To: bug-gnu-emacs <at> gnu.org
> Cc: Spencer Baugh <sbaugh <at> janestreet.com>, 66509-done <at> debbugs.gnu.org
> From: Stefan Kangas <stefankangas <at> gmail.com>
> Date: Wed, 18 Sep 2024 18:56:37 -0700
> 
> Version: 31.1
> 
> Artur Malabarba <arturmalabarba <at> gmail.com> writes:
> 
> > Hi all,
> >
> > The patch looks good.
> 
> Thanks.
> 
> > Stefan, would you mind applying it? (It'll take a while for me to get
> > everything configured.)
> 
> Pushed to master (commit ae4171efdc6).
> 
> I'm therefore closing this bug report.

Thanks, but could we perhaps have tests for this new functionality?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Fri, 27 Sep 2024 07:18:01 GMT) Full text and rfc822 format available.

Message #29 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: sbaugh <at> janestreet.com, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Fri, 27 Sep 2024 00:15:37 -0700
reopen 66509
thanks

>> Pushed to master (commit ae4171efdc6).
>>
>> I'm therefore closing this bug report.

I'm reopening this bug report, based on this request from Robert Pluim:

> Would it be possible to update the elisp manual as well (and maybe add
> an entry to NEWS)?

I think that should be done also, so I'm reopening the bug to track that
work.  Spencer, do you think you could you please address it?

Bonus points for adding tests, as requested by Eli.

Thanks in advance.




bug No longer marked as fixed in versions 31.1 and reopened. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 27 Sep 2024 07:18:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Wed, 12 Feb 2025 04:16:02 GMT) Full text and rfc822 format available.

Message #34 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefankangas <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: sbaugh <at> janestreet.com, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Tue, 11 Feb 2025 20:14:57 -0800
Stefan Kangas <stefankangas <at> gmail.com> writes:

> reopen 66509
> thanks
>
>>> Pushed to master (commit ae4171efdc6).
>>>
>>> I'm therefore closing this bug report.
>
> I'm reopening this bug report, based on this request from Robert Pluim:
>
>> Would it be possible to update the elisp manual as well (and maybe add
>> an entry to NEWS)?
>
> I think that should be done also, so I'm reopening the bug to track that
> work.  Spencer, do you think you could you please address it?
>
> Bonus points for adding tests, as requested by Eli.

Ping!




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Thu, 28 Aug 2025 19:06:02 GMT) Full text and rfc822 format available.

Message #37 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: Stefan Kangas <stefankangas <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Thu, 28 Aug 2025 15:05:08 -0400
[Message part 1 (text/plain, inline)]
Stefan Kangas <stefankangas <at> gmail.com> writes:

> Stefan Kangas <stefankangas <at> gmail.com> writes:
>
>> reopen 66509
>> thanks
>>
>>>> Pushed to master (commit ae4171efdc6).
>>>>
>>>> I'm therefore closing this bug report.
>>
>> I'm reopening this bug report, based on this request from Robert Pluim:
>>
>>> Would it be possible to update the elisp manual as well (and maybe add
>>> an entry to NEWS)?
>>
>> I think that should be done also, so I'm reopening the bug to track that
>> work.  Spencer, do you think you could you please address it?
>>
>> Bonus points for adding tests, as requested by Eli.
>
> Ping!

Done in attached patch.

[0001-Document-and-test-let-alist-support-for-indexing.patch (text/x-patch, inline)]
From 82b4cc3ccc1ff2f8e82ff910ffa16fcf48eeefb4 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> janestreet.com>
Date: Thu, 28 Aug 2025 15:04:39 -0400
Subject: [PATCH] Document and test let-alist support for indexing

* etc/NEWS: Announce let-alist support for indexing. (bug#66509)
* test/lisp/emacs-lisp/let-alist-tests.el (let-alist-numbers):
Add a test for let-alist's support for indexing.
---
 etc/NEWS                                |  6 ++++++
 test/lisp/emacs-lisp/let-alist-tests.el | 11 +++++++++++
 2 files changed, 17 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 058f0e896eb..1a9fe46d269 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2791,6 +2791,12 @@ function 'load-path-filter-cache-directory-files', calling 'load' will
 cache the directories it scans and their files, and the following
 lookups should be faster.
 
++++
+** 'let-alist' supports indexing into lists
+The macro 'let-alist' now interprets symbols containing numbers as list
+indexes.  For example, '.key.0' looks up 'key' in the alist and then
+returns its first element.
+
 ** Lexical binding
 
 ---
diff --git a/test/lisp/emacs-lisp/let-alist-tests.el b/test/lisp/emacs-lisp/let-alist-tests.el
index 988b05b488c..b23178f5467 100644
--- a/test/lisp/emacs-lisp/let-alist-tests.el
+++ b/test/lisp/emacs-lisp/let-alist-tests.el
@@ -100,4 +100,15 @@ let-alist--vectors
                    `[,(+ .a) ,(+ .a .b .b)])
                  [1 5])))
 
+(ert-deftest let-alist-numbers ()
+  "Check that .num indexes into lists."
+  (should (equal
+           (let-alist
+               '(((a . val1) (b . (nil val2)))
+                 (c . (val3)))
+             (list .0 .0.a .0.b.1 .c.0))
+           ;; .0 is interpreted as a number, so we can't use `let-alist'
+           ;; to do indexing alone.  Everything else works though.
+           '(0.0 val1 val2 val3))))
+
 ;;; let-alist-tests.el ends here
-- 
2.43.7


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Fri, 29 Aug 2025 05:46:03 GMT) Full text and rfc822 format available.

Message #40 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Spencer Baugh <sbaugh <at> janestreet.com>
Cc: stefankangas <at> gmail.com, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Fri, 29 Aug 2025 08:44:58 +0300
> From: Spencer Baugh <sbaugh <at> janestreet.com>
> Cc: Eli Zaretskii <eliz <at> gnu.org>,  66509 <at> debbugs.gnu.org
> Date: Thu, 28 Aug 2025 15:05:08 -0400
> 
> Stefan Kangas <stefankangas <at> gmail.com> writes:
> 
> > Stefan Kangas <stefankangas <at> gmail.com> writes:
> >
> >> reopen 66509
> >> thanks
> >>
> >>>> Pushed to master (commit ae4171efdc6).
> >>>>
> >>>> I'm therefore closing this bug report.
> >>
> >> I'm reopening this bug report, based on this request from Robert Pluim:
> >>
> >>> Would it be possible to update the elisp manual as well (and maybe add
> >>> an entry to NEWS)?
> >>
> >> I think that should be done also, so I'm reopening the bug to track that
> >> work.  Spencer, do you think you could you please address it?
> >>
> >> Bonus points for adding tests, as requested by Eli.
> >
> > Ping!
> 
> Done in attached patch.

Thanks.

> ++++
> +** 'let-alist' supports indexing into lists

Heading lines in NEWS should end in a period.

> +The macro 'let-alist' now interprets symbols containing numbers as list
> +indexes.  For example, '.key.0' looks up 'key' in the alist and then

"indices", I guess?

Also, I see no patch for the ELisp manual, although the "+++" mark in
NEWS seems to imply that the manual was updated.  But the current
manual doesn't seem to say anything about keys that include indices,
or did I miss something?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#66509; Package emacs. (Fri, 29 Aug 2025 13:20:02 GMT) Full text and rfc822 format available.

Message #43 received at 66509 <at> debbugs.gnu.org (full text, mbox):

From: Spencer Baugh <sbaugh <at> janestreet.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: stefankangas <at> gmail.com, 66509 <at> debbugs.gnu.org
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Fri, 29 Aug 2025 09:19:30 -0400
[Message part 1 (text/plain, inline)]
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Spencer Baugh <sbaugh <at> janestreet.com>
>> Cc: Eli Zaretskii <eliz <at> gnu.org>,  66509 <at> debbugs.gnu.org
>> Date: Thu, 28 Aug 2025 15:05:08 -0400
>> 
>> Stefan Kangas <stefankangas <at> gmail.com> writes:
>> 
>> > Stefan Kangas <stefankangas <at> gmail.com> writes:
>> >
>> >> reopen 66509
>> >> thanks
>> >>
>> >>>> Pushed to master (commit ae4171efdc6).
>> >>>>
>> >>>> I'm therefore closing this bug report.
>> >>
>> >> I'm reopening this bug report, based on this request from Robert Pluim:
>> >>
>> >>> Would it be possible to update the elisp manual as well (and maybe add
>> >>> an entry to NEWS)?
>> >>
>> >> I think that should be done also, so I'm reopening the bug to track that
>> >> work.  Spencer, do you think you could you please address it?
>> >>
>> >> Bonus points for adding tests, as requested by Eli.
>> >
>> > Ping!
>> 
>> Done in attached patch.
>
> Thanks.
>
>> ++++
>> +** 'let-alist' supports indexing into lists
>
> Heading lines in NEWS should end in a period.

Fixed.

>> +The macro 'let-alist' now interprets symbols containing numbers as list
>> +indexes.  For example, '.key.0' looks up 'key' in the alist and then
>
> "indices", I guess?

Fixed.

> Also, I see no patch for the ELisp manual, although the "+++" mark in
> NEWS seems to imply that the manual was updated.  But the current
> manual doesn't seem to say anything about keys that include indices,
> or did I miss something?

Oops, fixed.

[0001-Document-and-test-let-alist-support-for-indexing.patch (text/x-patch, inline)]
From 9284a437c495c2d166da265917efb1b4a4072925 Mon Sep 17 00:00:00 2001
From: Spencer Baugh <sbaugh <at> janestreet.com>
Date: Thu, 28 Aug 2025 15:04:39 -0400
Subject: [PATCH] Document and test let-alist support for indexing

* etc/NEWS: Announce let-alist support for indexing. (bug#66509)
* test/lisp/emacs-lisp/let-alist-tests.el (let-alist-numbers):
Add a test for let-alist's support for indexing.
* doc/lispref/lists.texi (Association Lists): Document indexing
with let-alist.
---
 doc/lispref/lists.texi                  | 13 +++++++++++++
 etc/NEWS                                |  6 ++++++
 test/lisp/emacs-lisp/let-alist-tests.el | 11 +++++++++++
 3 files changed, 30 insertions(+)

diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi
index 37a07421e94..81edcc63d5b 100644
--- a/doc/lispref/lists.texi
+++ b/doc/lispref/lists.texi
@@ -1934,6 +1934,19 @@ Association Lists
 Nesting @code{let-alist} inside each other is allowed, but the code in
 the inner @code{let-alist} can't access the variables bound by the
 outer @code{let-alist}.
+
+Indexing into lists is also supported:
+
+@lisp
+(setq colors '((rose . red) (lily . (yellow pink))))
+(let-alist colors .lily.1)
+     @result{} pink
+@end lisp
+
+Note that forms like @samp{.0} or @samp{.3} are interpreted as numbers
+rather than as symbols, so they won't be bound to the corresponding
+values in ALIST.
+
 @end defmac
 
 @node Property Lists
diff --git a/etc/NEWS b/etc/NEWS
index 058f0e896eb..12f3a7f1207 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2791,6 +2791,12 @@ function 'load-path-filter-cache-directory-files', calling 'load' will
 cache the directories it scans and their files, and the following
 lookups should be faster.
 
++++
+** 'let-alist' supports indexing into lists.
+The macro 'let-alist' now interprets symbols containing numbers as list
+indices.  For example, '.key.0' looks up 'key' in the alist and then
+returns its first element.
+
 ** Lexical binding
 
 ---
diff --git a/test/lisp/emacs-lisp/let-alist-tests.el b/test/lisp/emacs-lisp/let-alist-tests.el
index 988b05b488c..b23178f5467 100644
--- a/test/lisp/emacs-lisp/let-alist-tests.el
+++ b/test/lisp/emacs-lisp/let-alist-tests.el
@@ -100,4 +100,15 @@ let-alist--vectors
                    `[,(+ .a) ,(+ .a .b .b)])
                  [1 5])))
 
+(ert-deftest let-alist-numbers ()
+  "Check that .num indexes into lists."
+  (should (equal
+           (let-alist
+               '(((a . val1) (b . (nil val2)))
+                 (c . (val3)))
+             (list .0 .0.a .0.b.1 .c.0))
+           ;; .0 is interpreted as a number, so we can't use `let-alist'
+           ;; to do indexing alone.  Everything else works though.
+           '(0.0 val1 val2 val3))))
+
 ;;; let-alist-tests.el ends here
-- 
2.43.7


Reply sent to Eli Zaretskii <eliz <at> gnu.org>:
You have taken responsibility. (Sat, 30 Aug 2025 11:02:03 GMT) Full text and rfc822 format available.

Notification sent to Spencer Baugh <sbaugh <at> janestreet.com>:
bug acknowledged by developer. (Sat, 30 Aug 2025 11:02:04 GMT) Full text and rfc822 format available.

Message #48 received at 66509-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Spencer Baugh <sbaugh <at> janestreet.com>
Cc: 66509-done <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: Re: bug#66509: 29.1.50; let-alist should support numeric indexing
Date: Sat, 30 Aug 2025 14:00:37 +0300
> From: Spencer Baugh <sbaugh <at> janestreet.com>
> Cc: stefankangas <at> gmail.com,  66509 <at> debbugs.gnu.org
> Date: Fri, 29 Aug 2025 09:19:30 -0400
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> >> From: Spencer Baugh <sbaugh <at> janestreet.com>
> >> Cc: Eli Zaretskii <eliz <at> gnu.org>,  66509 <at> debbugs.gnu.org
> >> Date: Thu, 28 Aug 2025 15:05:08 -0400
> >> 
> >> Stefan Kangas <stefankangas <at> gmail.com> writes:
> >> 
> >> > Stefan Kangas <stefankangas <at> gmail.com> writes:
> >> >
> >> >> reopen 66509
> >> >> thanks
> >> >>
> >> >>>> Pushed to master (commit ae4171efdc6).
> >> >>>>
> >> >>>> I'm therefore closing this bug report.
> >> >>
> >> >> I'm reopening this bug report, based on this request from Robert Pluim:
> >> >>
> >> >>> Would it be possible to update the elisp manual as well (and maybe add
> >> >>> an entry to NEWS)?
> >> >>
> >> >> I think that should be done also, so I'm reopening the bug to track that
> >> >> work.  Spencer, do you think you could you please address it?
> >> >>
> >> >> Bonus points for adding tests, as requested by Eli.
> >> >
> >> > Ping!
> >> 
> >> Done in attached patch.
> >
> > Thanks.
> >
> >> ++++
> >> +** 'let-alist' supports indexing into lists
> >
> > Heading lines in NEWS should end in a period.
> 
> Fixed.
> 
> >> +The macro 'let-alist' now interprets symbols containing numbers as list
> >> +indexes.  For example, '.key.0' looks up 'key' in the alist and then
> >
> > "indices", I guess?
> 
> Fixed.
> 
> > Also, I see no patch for the ELisp manual, although the "+++" mark in
> > NEWS seems to imply that the manual was updated.  But the current
> > manual doesn't seem to say anything about keys that include indices,
> > or did I miss something?
> 
> Oops, fixed.

Thanks, now installed on the master branch, and closing the bug.




This bug report was last modified 19 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.