From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.devel Subject: Re: master 12f63c18f6 1/2: Add new macro 'while-let' Date: Wed, 28 Sep 2022 15:25:24 +0000 Message-ID: <87ill7ldwb.fsf@posteo.net> References: <166436449368.11560.17915607619196292155@vcs2.savannah.gnu.org> <20220928112814.B0924C12D9B@vcs2.savannah.gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5822"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Lars Ingebrigtsen To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Sep 28 18:20:07 2022 Return-path: Envelope-to: ged-emacs-devel@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 1odZn4-0001GS-Nn for ged-emacs-devel@m.gmane-mx.org; Wed, 28 Sep 2022 18:20:06 +0200 Original-Received: from localhost ([::1]:52146 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1odZn3-0002cl-HL for ged-emacs-devel@m.gmane-mx.org; Wed, 28 Sep 2022 12:20:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odYwJ-0003fp-Ie for emacs-devel@gnu.org; Wed, 28 Sep 2022 11:25:35 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:57975) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1odYwG-0007zI-9D for emacs-devel@gnu.org; Wed, 28 Sep 2022 11:25:35 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 55D43240028 for ; Wed, 28 Sep 2022 17:25:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1664378726; bh=UbwJvV2ZfOMK9K5PStVd4egXeXRUBPn0shV53OU3/O0=; h=From:To:Cc:Subject:Autocrypt:Date:From; b=SdKIthSUdTzXpVSx3nvhYNb1BSP1bngGLj5d3cXhmcgnhFb3RT3WLYWXrgJ30rRIC eRMAprv5bOKF5r48NfdxC9TPx2DYqswATJy2ORMB9vZZ/k+hpnWFT2uKP75vCbuInL 5Ao2lkaJG0+ugavHJS1ogQLIJPk4mp2AkainZihml88oyMCQuldF8SOcv69J8nkUKZ VIYj2fw6lwTknbityqVTNaUpFS46QISexdnNKgJlx1t7qxss5eyGMoCuXNRVcnzDHb DpEgRVAmF26dKvGeAODj75oTGuxoisYRO6d/Xjiqp8e5LjrD/AyKRTqptv9y9OzKzN Hh2utZO9rqwEw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Md0g94dr4z9rxB; Wed, 28 Sep 2022 17:25:25 +0200 (CEST) In-Reply-To: <20220928112814.B0924C12D9B@vcs2.savannah.gnu.org> (Lars Ingebrigtsen's message of "Wed, 28 Sep 2022 07:28:14 -0400 (EDT)") Autocrypt: addr=philipk@posteo.net; prefer-encrypt=nopreference; keydata= mDMEYHHqUhYJKwYBBAHaRw8BAQdAp3GdmYJ6tm5McweY6dEvIYIiry+Oz9rU4MH6NHWK0Ee0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiQBBMWCAA4FiEEDM2H44ZoPt9Ms0eHtVrAHPRh1FwFAmBx6lICGwMFCwkIBwIGFQoJ CAsCBBYCAwECHgECF4AACgkQtVrAHPRh1FyTkgEAjlbGPxFchvMbxzAES3r8QLuZgCxeAXunM9gh io0ePtUBALVhh9G6wIoZhl0gUCbQpoN/UJHI08Gm1qDob5zDxnIHuDgEYHHqUhIKKwYBBAGXVQEF AQEHQNcRB+MUimTMqoxxMMUERpOR+Q4b1KgncDZkhrO2ql1tAwEIB4h4BBgWCAAgFiEEDM2H44Zo Pt9Ms0eHtVrAHPRh1FwFAmBx6lICGwwACgkQtVrAHPRh1Fw1JwD/Qo7kvtib8jy7puyWrSv0MeTS g8qIxgoRWJE/KKdkCLEA/jb9b9/g8nnX+UcwHf/4VfKsjExlnND3FrBviXUW6NcB Received-SPF: pass client-ip=185.67.36.65; envelope-from=philipk@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:296406 Archived-At: Lars Ingebrigtsen writes: > branch: master > commit 12f63c18f6d5a886f62f10b4c8de8de3509e52df > Author: Lars Ingebrigtsen > Commit: Lars Ingebrigtsen > > Add new macro 'while-let' > > * doc/lispref/control.texi (Conditionals): Document > when-let/if-let/while-let. > * lisp/subr.el (while-let): New macro. > --- > doc/lispref/control.texi | 42 ++++++++++++++++++++++++++++++++++++++++++ > etc/NEWS | 4 ++++ > lisp/subr.el | 13 +++++++++++++ > 3 files changed, 59 insertions(+) > > diff --git a/doc/lispref/control.texi b/doc/lispref/control.texi > index ee2acdb002..9635b335bc 100644 > --- a/doc/lispref/control.texi > +++ b/doc/lispref/control.texi > @@ -294,6 +294,48 @@ For example: > @end group > @end example > > +If can be convenient to bind variables in conjunction with using a > +conditional. It's often the case that you do a computation, and then > +want to do something with that computation if it's non-@code{nil}. > +The straightforward way to do that is to just write, for instance: > + > +@example > +(let ((result1 (do-computation))) > + (when result1 > + (let ((result2 (do-more result1))) > + (when result2 > + (do-something result2))))) > +@end example > + > +Since this is a very common pattern, Emacs provides a number of macros > +to make this easier and more readable. The above can be written the > +following way instead: > + > +@example > +(when-let ((result1 (do-computation)) > + (result2 (do-more result1))) > + (do-something result2)) > +@end example > + > +There's a number of variations on this theme, and they're briefly > +described below. > + > +@defmac if-let spec then-form else-forms... > +Evaluate each binding in @var{spec} in turn, like in @code{let*} > +(@pxref{Local Variables}, stopping if a binding value is @code{nil}. > +If all are non-@code{nil}, return the value of @var{then-form}, > +otherwise the last form in @var{else-forms}. > +@end defmac > + > +@defmac when-let spec then-forms... > +Like @code{if-let}, but without @var{else-forms}. > +@end defmac > + > +@defmac while-let spec then-forms... > +Like @code{when-let}, but repeat until a binding in @var{spec} is > +@code{nil}. The return value is always @code{nil}. > +@end defmac > + > @node Combining Conditions > @section Constructs for Combining Conditions > @cindex combining conditions > diff --git a/etc/NEWS b/etc/NEWS > index b85975944a..e70f9be546 100644 > --- a/etc/NEWS > +++ b/etc/NEWS > @@ -3012,6 +3012,10 @@ The following generalized variables have been made obsolete: > > * Lisp Changes in Emacs 29.1 > > ++++ > +** New macro 'while-let'. > +This is like 'when-let', but repeats until a binding form is nil. > + > +++ > ** New function 'make-obsolete-generalized-variable'. > This can be used to mark setters used by 'setf' as obsolete, and the > diff --git a/lisp/subr.el b/lisp/subr.el > index 26fba4771b..2a8fc46a9f 100644 > --- a/lisp/subr.el > +++ b/lisp/subr.el > @@ -2514,7 +2514,20 @@ The variable list SPEC is the same as in `if-let'." > (declare (indent 1) (debug if-let)) > (list 'if-let spec (macroexp-progn body))) > > +(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. > +If all bindings are non-nil, eval BODY and repeat. > > +The variable list SPEC is the same as in `if-let'." > + (declare (indent 1) (debug if-let)) > + (let ((done (gensym "done"))) > + `(catch ',done > + (while t > + (if-let ,spec Is `if-let' used intentionally here, or would if-let* be better? > + (progn > + ,@body) > + (throw ',done nil)))))) > > ;; PUBLIC: find if the current mode derives from another. >