unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
@ 2022-09-28 12:04 Visuwesh
  0 siblings, 0 replies; 10+ messages in thread
From: Visuwesh @ 2022-09-28 12:04 UTC (permalink / raw)
  To: emacs-devel; +Cc: Lars Ingebrigtsen


>branch: master
>commit 12f63c18f6d5a886f62f10b4c8de8de3509e52df
>Author: Lars Ingebrigtsen <larsi@gnus.org>
>Commit: Lars Ingebrigtsen <larsi@gnus.org>
>
>    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
  ^^
Typo: this should be "It".

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



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
       [not found] ` <20220928112814.B0924C12D9B@vcs2.savannah.gnu.org>
@ 2022-09-28 15:25   ` Philip Kaludercic
  2022-09-29 10:29     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2022-09-28 15:25 UTC (permalink / raw)
  To: emacs-devel; +Cc: Lars Ingebrigtsen

Lars Ingebrigtsen <larsi@gnus.org> writes:

> branch: master
> commit 12f63c18f6d5a886f62f10b4c8de8de3509e52df
> Author: Lars Ingebrigtsen <larsi@gnus.org>
> Commit: Lars Ingebrigtsen <larsi@gnus.org>
>
>     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:
>  \f
>  * 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.
>  



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-09-28 15:25   ` master 12f63c18f6 1/2: Add new macro 'while-let' Philip Kaludercic
@ 2022-09-29 10:29     ` Lars Ingebrigtsen
  2022-09-29 11:42       ` Philip Kaludercic
  0 siblings, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-29 10:29 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel

Philip Kaludercic <philipk@posteo.net> writes:

> Is `if-let' used intentionally here, or would if-let* be better?

The latter.  Now fixed.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-09-29 10:29     ` Lars Ingebrigtsen
@ 2022-09-29 11:42       ` Philip Kaludercic
  2022-09-29 11:48         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2022-09-29 11:42 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Philip Kaludercic <philipk@posteo.net> writes:
>
>> Is `if-let' used intentionally here, or would if-let* be better?
>
> The latter.  Now fixed.

In that case ought the macro not be called `while-let*'?



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-09-29 11:42       ` Philip Kaludercic
@ 2022-09-29 11:48         ` Lars Ingebrigtsen
  2022-10-15 14:12           ` Philip Kaludercic
  0 siblings, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-29 11:48 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel

Philip Kaludercic <philipk@posteo.net> writes:

> In that case ought the macro not be called `while-let*'?

Nope.  We're pretending that the * versions of these macros don't exist
(by not mentioning them in the manual), and we're likewise pretending
that if-let doesn't have wider semantics than the * version (by not
mentioning that, either).




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-09-29 11:48         ` Lars Ingebrigtsen
@ 2022-10-15 14:12           ` Philip Kaludercic
  2022-10-16  7:45             ` Lars Ingebrigtsen
  0 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2022-10-15 14:12 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Philip Kaludercic <philipk@posteo.net> writes:
>
>> In that case ought the macro not be called `while-let*'?
>
> Nope.  We're pretending that the * versions of these macros don't exist
> (by not mentioning them in the manual), and we're likewise pretending
> that if-let doesn't have wider semantics than the * version (by not
> mentioning that, either).

On that topic, why do we have `and-let*' and no `and-let'?



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-10-15 14:12           ` Philip Kaludercic
@ 2022-10-16  7:45             ` Lars Ingebrigtsen
  2022-10-16  9:26               ` Philip Kaludercic
  0 siblings, 1 reply; 10+ messages in thread
From: Lars Ingebrigtsen @ 2022-10-16  7:45 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel

Philip Kaludercic <philipk@posteo.net> writes:

> On that topic, why do we have `and-let*' and no `and-let'?

I'd rather ask "why do we have and-let at all"?  😀

If you look at the in-tree usages of and-let*, most of them should
clearly have been when-let instead, so I think it was a mistake to add
and-let.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-10-16  7:45             ` Lars Ingebrigtsen
@ 2022-10-16  9:26               ` Philip Kaludercic
  2022-10-16 22:59                 ` Sean Whitton
  0 siblings, 1 reply; 10+ messages in thread
From: Philip Kaludercic @ 2022-10-16  9:26 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: emacs-devel

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Philip Kaludercic <philipk@posteo.net> writes:
>
>> On that topic, why do we have `and-let*' and no `and-let'?
>
> I'd rather ask "why do we have and-let at all"?  😀
>
> If you look at the in-tree usages of and-let*, most of them should
> clearly have been when-let instead, so I think it was a mistake to add
> and-let.

I have recently started appreciating `and-let*' when I want to make it
explicit that the last binding is the return value, but I guess that any

    (and-let* ((foo bar) ... (baz qux)))

is the same as

    (when-let ((foo bar) ...) (baz qux))



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-10-16  9:26               ` Philip Kaludercic
@ 2022-10-16 22:59                 ` Sean Whitton
  2022-10-17  0:14                   ` [External] : " Drew Adams
  0 siblings, 1 reply; 10+ messages in thread
From: Sean Whitton @ 2022-10-16 22:59 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: Lars Ingebrigtsen, emacs-devel

Hello,

On Sun 16 Oct 2022 at 09:26AM GMT, Philip Kaludercic wrote:

> Lars Ingebrigtsen <larsi@gnus.org> writes:
>
>> Philip Kaludercic <philipk@posteo.net> writes:
>>
>>> On that topic, why do we have `and-let*' and no `and-let'?
>>
>> I'd rather ask "why do we have and-let at all"?  😀
>>
>> If you look at the in-tree usages of and-let*, most of them should
>> clearly have been when-let instead, so I think it was a mistake to add
>> and-let.
>
> I have recently started appreciating `and-let*' when I want to make it
> explicit that the last binding is the return value, but I guess that any
>
>     (and-let* ((foo bar) ... (baz qux)))
>
> is the same as
>
>     (when-let ((foo bar) ...) (baz qux))

Yeah, that's a Lisp convention I learned from Magit's maintainer --
when/unless for side-effects, and/or for return value.  I appreciate
having and-let for this reason.

-- 
Sean Whitton



^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [External] : Re: master 12f63c18f6 1/2: Add new macro 'while-let'
  2022-10-16 22:59                 ` Sean Whitton
@ 2022-10-17  0:14                   ` Drew Adams
  0 siblings, 0 replies; 10+ messages in thread
From: Drew Adams @ 2022-10-17  0:14 UTC (permalink / raw)
  To: Sean Whitton, Philip Kaludercic; +Cc: Lars Ingebrigtsen, emacs-devel@gnu.org

> Yeah, that's a Lisp convention I learned from Magit's maintainer --
> when/unless for side-effects, and/or for return value.  I appreciate
> having and-let for this reason.

It's a very old convention (and a good one) - even
called out in "Common Lisp The Language" (from the
beginning).

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-10-17  0:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <166436449368.11560.17915607619196292155@vcs2.savannah.gnu.org>
     [not found] ` <20220928112814.B0924C12D9B@vcs2.savannah.gnu.org>
2022-09-28 15:25   ` master 12f63c18f6 1/2: Add new macro 'while-let' Philip Kaludercic
2022-09-29 10:29     ` Lars Ingebrigtsen
2022-09-29 11:42       ` Philip Kaludercic
2022-09-29 11:48         ` Lars Ingebrigtsen
2022-10-15 14:12           ` Philip Kaludercic
2022-10-16  7:45             ` Lars Ingebrigtsen
2022-10-16  9:26               ` Philip Kaludercic
2022-10-16 22:59                 ` Sean Whitton
2022-10-17  0:14                   ` [External] : " Drew Adams
2022-09-28 12:04 Visuwesh

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).