emacs-devel
[Top][All Lists]
Advanced

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

RE: [External] : Re: Instead of pcase


From: Drew Adams
Subject: RE: [External] : Re: Instead of pcase
Date: Tue, 12 Dec 2023 15:18:05 +0000

> I think whoever wrote that code in byte-optimize-letX was fascinated
> by the challenge of writing as much as possible in pcase language
> and minimizing use of Lisp.
> 
> Here's my version, which I think is a lot clearer.
> 
> It illustrates some of the flexibility of cond*:
> * Use of ordinary Lisp fir conditions,
> * Matching against different objects.
> * Clauses that bind and then drop through.
> 
> (defun byte-optimize-letX (form)
>   (cond*
>     ;; Bindings list is empty.
>     (:match (`(,_ () . ,body) form)
>      `(progn . ,body))
> 
>     ;; Decompose the form
>     (:match (`(,head ,bindings . ,body) form))
> 
>     ;; Body is empty or just contains a constant.
>     (:match ((or `() `(,(macroexp-const-p const))) body)
>      ;; If the match succeeds, it always binds CONST.
>      (if (eq head 'let)
>          `(progn ,@(mapcar #'cadr bindings) ,const)
>        `(,head ,(butlast bindings) ,(cadar (last bindings)) ,const)))
> 
>     ;; Body does nothing but return the last variable in bindings.
>     ((let ((last-var (car-safe (car (last bindings)))))
>        ;; Next line could be written using `match',
>        ;; but the clarity of this is worth one cons.
>        (and (symbolp last-var) (equal body (list last-var))))
>      (if (eq head 'let)
>          `(progn ,@(mapcar #'cadr bindings))
>        `(,head ,(butlast bindings) ,(cadar (last bindings)))))
> 
>     (t form)))


You might want to show the original, pcase,
version at the same time, for comparison.
___

It might be clearer if clauses that can end
the `cond*' are easily distinguished from
those that always continue on to the next
clause - their return value doesn't matter to
the control flow (e.g., they just match/bind).

Or is the former always the case if a clause
contains two successive sexps?  E.g., is the
second clause above the only one that always
"falls through" after performing its action
(matching & binding)?  If so, the absence or
presence of a second sexp in a clause
distinguishes the two cases enough, I guess.

reply via email to

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