unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Checking conditions unrelated to expression
@ 2024-12-08 12:05 Heime via Users list for the GNU Emacs text editor
  2024-12-08 12:31 ` Thibaut Verron
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-12-08 12:05 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor

It looks like pcase can only be used with some variable condition

This will check condition of featr

  (pcase featr
     ('this (do-this))
     ('that (do-that))
     (_ (message "some message"))

What if I want to check a number of conditions, not all dependent upon
featr, what should one do? 

Suppose I want to test (eq duck 'quack), cannot use a pcase because tests
are dependent upon featr.

  (pcase featr
     ('this (do-this))
     ('that (do-that))
     (_ (message "some message"))






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

* Re: Checking conditions unrelated to expression
  2024-12-08 12:05 Checking conditions unrelated to expression Heime via Users list for the GNU Emacs text editor
@ 2024-12-08 12:31 ` Thibaut Verron
  2024-12-08 14:49   ` Heime via Users list for the GNU Emacs text editor
  2024-12-08 16:01 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-12-08 19:19 ` Michael Heerdegen via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 5+ messages in thread
From: Thibaut Verron @ 2024-12-08 12:31 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

Le dim. 8 déc. 2024 à 13:06, Heime via Users list for the GNU Emacs text
editor <help-gnu-emacs@gnu.org> a écrit :

> It looks like pcase can only be used with some variable condition
>
> This will check condition of featr
>
>   (pcase featr
>      ('this (do-this))
>      ('that (do-that))
>      (_ (message "some message"))
>
> What if I want to check a number of conditions, not all dependent upon
> featr, what should one do?
>
> Suppose I want to test (eq duck 'quack), cannot use a pcase because tests
> are dependent upon featr.
>
>   (pcase featr
>      ('this (do-this))
>      ('that (do-that))
>      (_ (message "some message"))
>

You can use a plain case-switch, rather than a pattern-matching one:

(cond
  ((eq featr 'this) (do-this))
  ((eq featr 'that) (do-that))
  ((eq duck 'quack) (do-duck))
  (t (message "blabla")))

https://www.gnu.org/s/emacs/manual/html_node/elisp/Conditionals.html

You can also replace the first two cases by a pattern-matching case, if
you'd like:

(cond
  (pcase featr
      ('this (do-this) t) ;; important to return non-nil in each branch
      ('that (do-that) t)
      (_ nil))
  ((eq duck 'quack) (do-duck))
   (t (message "blabla")))


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

* Re: Checking conditions unrelated to expression
  2024-12-08 12:31 ` Thibaut Verron
@ 2024-12-08 14:49   ` Heime via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 5+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-12-08 14:49 UTC (permalink / raw)
  To: thibaut.verron; +Cc: Heime via Users list for the GNU Emacs text editor






Sent with Proton Mail secure email.

On Monday, December 9th, 2024 at 12:31 AM, Thibaut Verron <thibaut.verron@gmail.com> wrote:

> Le dim. 8 déc. 2024 à 13:06, Heime via Users list for the GNU Emacs text
> editor help-gnu-emacs@gnu.org a écrit :
> 
> > It looks like pcase can only be used with some variable condition
> > 
> > This will check condition of featr
> > 
> > (pcase featr
> > ('this (do-this))
> > ('that (do-that))
> > (_ (message "some message"))
> > 
> > What if I want to check a number of conditions, not all dependent upon
> > featr, what should one do?
> > 
> > Suppose I want to test (eq duck 'quack), cannot use a pcase because tests
> > are dependent upon featr.
> > 
> > (pcase featr
> > ('this (do-this))
> > ('that (do-that))
> > (_ (message "some message"))
> 
> 
> You can use a plain case-switch, rather than a pattern-matching one:
> 
> (cond
> ((eq featr 'this) (do-this))
> ((eq featr 'that) (do-that))
> ((eq duck 'quack) (do-duck))
> (t (message "blabla")))
> 
> https://www.gnu.org/s/emacs/manual/html_node/elisp/Conditionals.html
> 
> You can also replace the first two cases by a pattern-matching case, if
> you'd like:
> 
> (cond
> (pcase featr
> ('this (do-this) t) ;; important to return non-nil in each branch
> ('that (do-that) t)
> (_ nil))
> ((eq duck 'quack) (do-duck))
> (t (message "blabla")))


But I cannot do the other way round, right?  Cannot have a pcase using featr
as EXP, but checking something else.






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

* Re: Checking conditions unrelated to expression
  2024-12-08 12:05 Checking conditions unrelated to expression Heime via Users list for the GNU Emacs text editor
  2024-12-08 12:31 ` Thibaut Verron
@ 2024-12-08 16:01 ` Stefan Monnier via Users list for the GNU Emacs text editor
  2024-12-08 19:19 ` Michael Heerdegen via Users list for the GNU Emacs text editor
  2 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-12-08 16:01 UTC (permalink / raw)
  To: help-gnu-emacs

> Suppose I want to test (eq duck 'quack), cannot use a pcase because tests
> are dependent upon featr.
>
>   (pcase featr
>      ('this (do-this))
>      ('that (do-that))
>      (_ (message "some message"))

Like Thibaut said, for this example `cond` would probably work best.
But if you have good reasons to use `pcase`, you *can* do what
you suggest.
E.g.:

   (pcase featr
      ('this (do-this))
      ('that (do-that))
      (_ (if (eq duck 'quack) (do-not) (message "some message"))))

or

   (pcase featr
      ('this (do-this))
      ('that (do-that))
      ((guard (eq duck 'quack)) (do-not))
      (_ (message "some message")))

or

   (pcase featr
      ('this (do-this))
      ('that (do-that))
      ((let 'quack duck) (do-not))
      (_ (message "some message")))

...


        Stefan




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

* Re: Checking conditions unrelated to expression
  2024-12-08 12:05 Checking conditions unrelated to expression Heime via Users list for the GNU Emacs text editor
  2024-12-08 12:31 ` Thibaut Verron
  2024-12-08 16:01 ` Stefan Monnier via Users list for the GNU Emacs text editor
@ 2024-12-08 19:19 ` Michael Heerdegen via Users list for the GNU Emacs text editor
  2 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen via Users list for the GNU Emacs text editor @ 2024-12-08 19:19 UTC (permalink / raw)
  To: help-gnu-emacs

Hello,

the only thing I want to add to Stefan's answer: the 'let' pattern can
be (despite the name) used as a full-featured "sub-pcase".  But also see
'app' etc. - just see the pattern descriptions in the fine docs.

Michael.




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

end of thread, other threads:[~2024-12-08 19:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-08 12:05 Checking conditions unrelated to expression Heime via Users list for the GNU Emacs text editor
2024-12-08 12:31 ` Thibaut Verron
2024-12-08 14:49   ` Heime via Users list for the GNU Emacs text editor
2024-12-08 16:01 ` Stefan Monnier via Users list for the GNU Emacs text editor
2024-12-08 19:19 ` Michael Heerdegen via Users list for the GNU Emacs text editor

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