[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pcase bindings in patterns with complicated logic

From: Ihor Radchenko
Subject: Re: pcase bindings in patterns with complicated logic
Date: Sun, 21 Jan 2024 14:06:42 +0000

Richard Stallman <rms@gnu.org> writes:

> I am not confident I understand app pattern, but if it does what I now
> think, the equivalent of (app (mapcar #'cdr) `(,_ ,_ ,c))) would be
> this:
>   (match* `(,_ ,_ ,c) (mapcar 'car  '((foo . 1) (bar . 2) (baz . 3))))


> What cond* cannot do is apply `and' to two different match* clauses
> which test different objects.  I could add that capability.

You understand correctly - `and' for different match* clauses is what I
pointed to.

> ... I could also add `app', if that is worth adding. Its absence is
> not a fundamental design issue, just that it dud not seem necessary.

`app' is actually a more limited version of what you can do when
combining multiple match* clauses - match* clauses can test completely
unrelated objects, while pcase's `app' can only test a transformed first
pcase argument.

So, I do not think that `app' is required if combining match* clauses is

> That example wan't a solution to a real problem, so I am not convinced
> this is a real gap in cond*.  If I see this sort of situation in a
> real problem, that will convince me I should do something about this.

I looked up the use of pcase's `app' pattern across the packages I use
and inside Emacs source code. There are only several trivial uses,

(defun engrave-faces-preset-style (faces)
  "Return the preset style for FACES, should it exist.
Unconditionally returns nil when FACES is default."
  (pcase faces
    ('default nil)
    ((pred symbolp)
     (assoc faces engrave-faces-preset-styles))
    ((and (pred listp) (app length 1)) ; <--- here
     (assoc (car faces) engrave-faces-preset-styles))))

(defun octave--indent-new-comment-line (orig &rest args)
  (pcase (syntax-ppss)
    ((app ppss-string-terminator ?\')
     (user-error "Cannot split a single-quoted string"))
    ((app ppss-string-terminator ?\")
     (insert octave-string-continuation-marker))
    ((pred (not ppss-comment-depth))
     (unless (octave-smie--in-parens-p)
       (insert " " octave-continuation-string))))
  (apply orig args)

and a couple of internal pcase uses that are there to extend pcase -
`rx--pcase-expand', `seq--make-pcase-bindings',
`map--make-pcase-bindings', and (pcase-defmacro eieio...)

Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]