GNU bug report logs - #48212
[PATCH] subr-x.el: Added `while-let` and `while-let*`

Previous Next

Package: emacs;

Reported by: Hu Lucius <orctarorga <at> gmail.com>

Date: Tue, 4 May 2021 00:24:02 UTC

Severity: wishlist

Tags: patch, wontfix

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 48212 in the body.
You can then email your comments to 48212 AT debbugs.gnu.org in the normal way.

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#48212; Package emacs. (Tue, 04 May 2021 00:24:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Hu Lucius <orctarorga <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 04 May 2021 00:24:02 GMT) Full text and rfc822 format available.

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

From: Hu Lucius <orctarorga <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: [PATCH] subr-x.el: Added `while-let` and `while-let*`
Date: Mon, 3 May 2021 23:29:26 +0000
[Message part 1 (text/plain, inline)]
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 <lebensterben <at> users.noreply.github.com>
---
 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
[Message part 2 (text/html, inline)]

Severity set to 'wishlist' from 'normal' Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Tue, 04 May 2021 15:20:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48212; Package emacs. (Wed, 12 May 2021 18:58:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Hu Lucius <orctarorga <at> gmail.com>
Cc: 48212 <at> debbugs.gnu.org
Subject: Re: bug#48212: [PATCH] subr-x.el: Added `while-let` and `while-let*`
Date: Wed, 12 May 2021 20:57:11 +0200
Hu Lucius <orctarorga <at> gmail.com> writes:

> 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) ...).

Thank you, but I think these macros don't really give enough
functionality to warrant adding them to Emacs -- in general, using
`cl-loop' when you have conditions like this makes more sense, since it
gives you more control over the conditions and the return values.

So I'm closing this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) wontfix. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Wed, 12 May 2021 18:58:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 48212 <at> debbugs.gnu.org and Hu Lucius <orctarorga <at> gmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Wed, 12 May 2021 18:58:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 10 Jun 2021 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 292 days ago.

Previous Next


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