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>

Full log


View this message in rfc822 format

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: 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


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.