From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Hu Lucius Newsgroups: gmane.emacs.bugs Subject: bug#48212: [PATCH] subr-x.el: Added `while-let` and `while-let*` Date: Mon, 3 May 2021 23:29:26 +0000 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000bed98505c175550d" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21689"; mail-complaints-to="usenet@ciao.gmane.io" To: 48212@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue May 04 02:27:33 2021 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 1ldiuS-0005Z3-Pb for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 04 May 2021 02:27:32 +0200 Original-Received: from localhost ([::1]:42836 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldiuR-0004a6-Ry for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 03 May 2021 20:27:31 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49900) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldir6-00031g-3y for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 20:24:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:38023) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldir4-0003GJ-G4 for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 20:24:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ldir4-0000q7-CA for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 20:24:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Hu Lucius Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 04 May 2021 00:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 48212 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16200878323215 (code B ref -1); Tue, 04 May 2021 00:24:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 4 May 2021 00:23:52 +0000 Original-Received: from localhost ([127.0.0.1]:49569 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ldiqu-0000pn-9L for submit@debbugs.gnu.org; Mon, 03 May 2021 20:23:52 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:46558) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ldi0U-0006dp-Ag for submit@debbugs.gnu.org; Mon, 03 May 2021 19:29:42 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39970) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldi0U-0007tf-2d for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 19:29:42 -0400 Original-Received: from mail-qv1-xf2c.google.com ([2607:f8b0:4864:20::f2c]:35419) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldi0R-0006nm-Rk for bug-gnu-emacs@gnu.org; Mon, 03 May 2021 19:29:41 -0400 Original-Received: by mail-qv1-xf2c.google.com with SMTP id q6so2412529qvb.2 for ; Mon, 03 May 2021 16:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=agEcfF/QQMy/mzias1/GakvbxAuNfxPdowVM4DNGy4U=; b=CYT6O+dOMn4QLJWdcAXd7y87nM/UBdwcJFVE41q13m28EsvbFpDbIPT5wl2LSQQlXj NUPblH/yvjmkjdbCOMXDmZOEDLzLwyBxBwKr8mKrNm5wQwX0ymGfWxfwk1VdmUp1urTE 8pkbR0HcFYf7Ij7BblQUObBN8CWvnd0w1u7bAOCIqTZbpTpVd8SYP+Wdi4jeAbo81Gye GcoLusBVNTeO/mzH717XBbUWN2fLZMhipQvouvuJOMrbJK4xU4y3U40/ZKVpqFMQrTbi dUnXpZvh0Bd3++joGMsz1rf13wnlQ3XOrazrO2SX9aGg/lFehgfNnxf0QULl7HjhHZz1 rMvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=agEcfF/QQMy/mzias1/GakvbxAuNfxPdowVM4DNGy4U=; b=R7dL/oBf/w7ELUlLP/e4F/GssrmJ4nIXnFgyogeelf52DGQ8yIabHMdSbdjnOSqiDb zZP5Z9dBeo04ivKAyk/ITGeey6X2NLH5wblUwOwTcv0vnaAC8KpFEepOdC+bi8CLn7cu d+2KCEchmBAn2p5OTXrZn4pPK8eBZjCzmqOuv2mIPky4+ZvckFPAntZMlayuT1v/j9YS gmCXEyggGCPc3w+C8yljsncmYJChtEExycqJb15t261vGM6XSu6Kqu0IoOFLMBuG++ps ZYllfo7s4xWiUWpu2VggwgmgeWft2GD4cGWDfjfYxYYbDAmuwEU7PCHNuivED1pVtPYG m0/Q== X-Gm-Message-State: AOAM533wQHxVN3R9C5QXPZYqMVQ2wVqUVqbk+JAQyMNXZIx6k79nOJaS I/Prvuk5SxOyK+h8pZcMCYCofuG3RxUF7tlbrzseXFyuSxA= X-Google-Smtp-Source: ABdhPJwH+QxO8cBIPODUeZBZN83atf8MAL7oVcEc6vlaOiiMccCsig0VCcDBnBz9HSgj1fezncGsPneQWkZAQdRqfOI= X-Received: by 2002:a0c:e450:: with SMTP id d16mr17614981qvm.46.1620084577457; Mon, 03 May 2021 16:29:37 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::f2c; envelope-from=orctarorga@gmail.com; helo=mail-qv1-xf2c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Mon, 03 May 2021 20:23:51 -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" Xref: news.gmane.io gmane.emacs.bugs:205565 Archived-At: --000000000000bed98505c175550d Content-Type: text/plain; charset="UTF-8" Hi, I've added two functions in `subr-x`, `while-let` and `while-let*`. `(while-let SPEC BODY)` evaluate each binding in turn and stop if a binding value is nil. BODY is evaluated repeatedly until a binding value is nil. `(while-let* VARLIST BODY)` is like `while-let` except VARLIST is of the form ((SYMBOL SOMETHING) ...). Example: Suppose we've a list, (setq xs '(1 2 3 4 5)), the following while-let form (while-let ((x (pop xs))) (print x)) is equivalent to (while (when-let ((x (pop xs))) (print x) t)) The last expression `t` is added so that regardless of the return value of the last form in BODY, the while loop continues, unless a binding value is nil. Signed-off-by: Lucius Hu --- lisp/emacs-lisp/subr-x.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 9c8c967ee9..3203cce455 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -156,6 +156,13 @@ are non-nil, then the result is non-nil." ,@(or body `(,res)))) `(let* () ,@(or body '(t)))))) +(defmacro while-let* (varlist &rest body) + "Bind variables according to VARLIST and conditionally evaluate BODY. +This is like `while-let' but doesn't handle a VARLIST of the form +\(SYMBOL SOMETHING) sepcially." + (declare (indent 1) (debug if-let*)) + (list 'while (list 'if-let* varlist (nconc (macroexp-progn body) (list t))))) + ;;;###autoload (defmacro if-let (spec then &rest else) "Bind variables according to SPEC and evaluate THEN or ELSE. @@ -193,6 +200,15 @@ The variable list SPEC is the same as in `if-let'." (declare (indent 1) (debug if-let)) (list 'if-let spec (macroexp-progn body))) +;;;###autoload +(defmacro while-let (spec &rest body) + "Bind variables according to SPEC and conditionally evaluate BODY. +Evaluate each binding in turn, stopping if a binding value is nil. +BODY is repeatedly evaluated until a binding value is nil. +The return value is always nil." + (declare (indent 1) (debug if-let)) + (list 'while (list 'if-let spec (nconc (macroexp-progn body) (list t))))) + (defsubst hash-table-empty-p (hash-table) "Check whether HASH-TABLE is empty (has 0 elements)." (zerop (hash-table-count hash-table))) -- 2.31.1 --000000000000bed98505c175550d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

I've added two funct= ions in `subr-x`, `while-let` and `while-let*`.

`(while-let SPEC BODY)` evaluate each binding in turn and stop if = a
binding value is nil.
BODY is evaluated repeatedly until a bindin= g value is nil.

`(while-let* VARLIST BODY)` is like `while-let` exce= pt VARLIST is of
the form ((SYMBOL SOMETHING) ...).

<= /div>
Example:
Suppose we've a list, (setq xs '(1= 2 3 4 5)), the following while-let form

(whil= e-let ((x (pop xs)))
=C2=A0=C2=A0 (print x))

=
is equivalent to

(while (when-let ((x (pop xs= )))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 (print x)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 t))

The last expres= sion `t` is added so that regardless of the return value
of the l= ast form in BODY, the while loop continues, unless a binding
valu= e is nil.

Signed-off-by: Lucius Hu <lebensterben@users.nore= ply.github.com>
---
=C2=A0lisp/emacs-lisp/subr-x.el | 16 +++++= +++++++++++
=C2=A01 file changed, 16 insertions(+)

diff --git a/l= isp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 9c8c967ee9..3= 203cce455 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lis= p/subr-x.el
@@ -156,6 +156,13 @@ are non-nil, then the result is non-nil= ."
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 ,@(or body `(,res))))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 `(let* () ,@(or body '(t))))))
=C2=A0
+(defmacro while-let* (= varlist &rest body)
+=C2=A0 "Bind variables according to VARLIS= T and conditionally evaluate BODY.
+This is like `while-let' but doe= sn't handle a VARLIST of the form
+\(SYMBOL SOMETHING) sepcially.&qu= ot;
+=C2=A0 (declare (indent 1) (debug if-let*))
+=C2=A0 (list 'w= hile (list 'if-let* varlist (nconc (macroexp-progn body) (list t)))))+
=C2=A0;;;###autoload
=C2=A0(defmacro if-let (spec then &rest = else)
=C2=A0=C2=A0 "Bind variables according to SPEC and evaluate T= HEN or ELSE.
@@ -193,6 +200,15 @@ The variable list SPEC is the same as = in `if-let'."
=C2=A0=C2=A0 (declare (indent 1) (debug if-let))<= br>=C2=A0=C2=A0 (list 'if-let spec (macroexp-progn body)))
=C2=A0+;;;###autoload
+(defmacro while-let (spec &rest body)
+=C2=A0 &= quot;Bind variables according to SPEC and conditionally evaluate BODY.
+= Evaluate each binding in turn, stopping if a binding value is nil.
+BODY= is repeatedly evaluated until a binding value is nil.
+The return value= is always nil."
+=C2=A0 (declare (indent 1) (debug if-let))
+= =C2=A0 (list 'while (list 'if-let spec (nconc (macroexp-progn body)= (list t)))))
+
=C2=A0(defsubst hash-table-empty-p (hash-table)
= =C2=A0=C2=A0 "Check whether HASH-TABLE is empty (has 0 elements)."= ;
=C2=A0=C2=A0 (zerop (hash-table-count hash-table)))
--
2.31.1
--000000000000bed98505c175550d--