all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* pcase without code duplication
@ 2021-04-23  2:45 michael-franzese
  2021-04-23  3:24 ` Óscar Fuentes
  0 siblings, 1 reply; 5+ messages in thread
From: michael-franzese @ 2021-04-23  2:45 UTC (permalink / raw)
  To: Help Gnu Emacs

I am using pcase, but am wondering whether I can have pcase
for 0 and 2 do the same things without adding a separate pcase
(0 and duplicating the statements.

(pcase orthogr-region
  (0 ... )    ; same operations as pcase 2
  (1 ... )
  (2 ... )    ; operations
  (_ ... ) )




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

* Re: pcase without code duplication
  2021-04-23  2:45 pcase without code duplication michael-franzese
@ 2021-04-23  3:24 ` Óscar Fuentes
  2021-04-23  3:45   ` michael-franzese
  2021-04-23  3:51   ` [External] : " Drew Adams
  0 siblings, 2 replies; 5+ messages in thread
From: Óscar Fuentes @ 2021-04-23  3:24 UTC (permalink / raw)
  To: help-gnu-emacs

michael-franzese@gmx.com writes:

> I am using pcase, but am wondering whether I can have pcase
> for 0 and 2 do the same things without adding a separate pcase
> (0 and duplicating the statements.
>
> (pcase orthogr-region
>   (0 ... )    ; same operations as pcase 2
>   (1 ... )
>   (2 ... )    ; operations
>   (_ ... ) )

(let ((xx 2))
  (pcase xx
    (1 (message "one"))
    ((or 0 2) (message "zero or two"))
    (_ (message "something else"))))




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

* Re: pcase without code duplication
  2021-04-23  3:24 ` Óscar Fuentes
@ 2021-04-23  3:45   ` michael-franzese
  2021-04-23  4:01     ` Stefan Monnier
  2021-04-23  3:51   ` [External] : " Drew Adams
  1 sibling, 1 reply; 5+ messages in thread
From: michael-franzese @ 2021-04-23  3:45 UTC (permalink / raw)
  To: Óscar Fuentes; +Cc: help-gnu-emacs


Can one also use nil as option in pcase.  What possibilities
exist for (_ ?

e.g.

(nil
((or 0 nil)
((or _ nil)  

Regards
Michael


> Sent: Friday, April 23, 2021 at 3:24 PM
> From: "Óscar Fuentes" <ofv@wanadoo.es>
> To: help-gnu-emacs@gnu.org
> Subject: Re: pcase without code duplication
>
> michael-franzese@gmx.com writes:
> 
> > I am using pcase, but am wondering whether I can have pcase
> > for 0 and 2 do the same things without adding a separate pcase
> > (0 and duplicating the statements.
> >
> > (pcase orthogr-region
> >   (0 ... )    ; same operations as pcase 2
> >   (1 ... )
> >   (2 ... )    ; operations
> >   (_ ... ) )
> 
> (let ((xx 2))
>   (pcase xx
>     (1 (message "one"))
>     ((or 0 2) (message "zero or two"))
>     (_ (message "something else"))))
> 
> 
>



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

* RE: [External] : Re: pcase without code duplication
  2021-04-23  3:24 ` Óscar Fuentes
  2021-04-23  3:45   ` michael-franzese
@ 2021-04-23  3:51   ` Drew Adams
  1 sibling, 0 replies; 5+ messages in thread
From: Drew Adams @ 2021-04-23  3:51 UTC (permalink / raw)
  To: Óscar Fuentes, help-gnu-emacs@gnu.org

> > I am using pcase, but am wondering whether I can have pcase
> > for 0 and 2 do the same things without adding a separate pcase
> > (0 and duplicating the statements.
> >
> > (pcase orthogr-region
> >   (0 ... )    ; same operations as pcase 2
> >   (1 ... )
> >   (2 ... )    ; operations
> >   (_ ... ) )
> 
> (let ((xx 2))
>   (pcase xx
>     (1 (message "one"))
>     ((or 0 2) (message "zero or two"))
>     (_ (message "something else"))))

And even ordinary cl-case (aka case) will do,
if that's all you're doing.

(cl-case xx
  (1     (message "one"))
  ((0 2) (message "zero or two"))
  (t     (message "something else")))




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

* Re: pcase without code duplication
  2021-04-23  3:45   ` michael-franzese
@ 2021-04-23  4:01     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2021-04-23  4:01 UTC (permalink / raw)
  To: help-gnu-emacs

> Can one also use nil as option in pcase.  What possibilities
> exist for (_ ?
>
> e.g.
>
> (nil

If you want to match a symbol, you need to quote it:

    ('nil (do-something))

> ((or 0 nil)

    ((or 0 'nil) (do-something-else))

Of course, you can also quote the 0 if you want, tho it makes no difference.

> ((or _ nil)  

This is redundant, but yes you can do

    ((or _ 'nil) (do-yet-another-thing))


-- Stefan




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

end of thread, other threads:[~2021-04-23  4:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-23  2:45 pcase without code duplication michael-franzese
2021-04-23  3:24 ` Óscar Fuentes
2021-04-23  3:45   ` michael-franzese
2021-04-23  4:01     ` Stefan Monnier
2021-04-23  3:51   ` [External] : " Drew Adams

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.