From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.bugs Subject: bug#66509: 29.1.50; let-alist should support numeric indexing Date: Thu, 12 Oct 2023 18:04:00 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: text/x-patch Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38878"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: 66509@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Oct 13 00:05:09 2023 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 1qr3np-0009pv-P9 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 13 Oct 2023 00:05:09 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qr3nR-00030a-8W; Thu, 12 Oct 2023 18:04:45 -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 1qr3nN-0002zv-Sd for bug-gnu-emacs@gnu.org; Thu, 12 Oct 2023 18:04:43 -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 1qr3nL-0002hX-O8 for bug-gnu-emacs@gnu.org; Thu, 12 Oct 2023 18:04:41 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qr3ni-0007EC-Ay for bug-gnu-emacs@gnu.org; Thu, 12 Oct 2023 18:05:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 12 Oct 2023 22:05:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66509 X-GNU-PR-Package: emacs Original-Received: via spool by 66509-submit@debbugs.gnu.org id=B66509.169714827127713 (code B ref 66509); Thu, 12 Oct 2023 22:05:02 +0000 Original-Received: (at 66509) by debbugs.gnu.org; 12 Oct 2023 22:04:31 +0000 Original-Received: from localhost ([127.0.0.1]:44408 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qr3nD-0007Cu-7d for submit@debbugs.gnu.org; Thu, 12 Oct 2023 18:04:31 -0400 Original-Received: from mxout5.mail.janestreet.com ([64.215.233.18]:60919) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qr3nA-0007Ca-AT for 66509@debbugs.gnu.org; Thu, 12 Oct 2023 18:04:30 -0400 In-Reply-To: (Spencer Baugh's message of "Thu, 12 Oct 2023 18:03:22 -0400") Content-Disposition: inline; filename=0001-Support-numeric-indexing-in-let-alist.patch 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:272326 Archived-At: >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)) -- 2.39.3