emacs-devel
[Top][All Lists]
Advanced

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

Re: Instead of pcase


From: Richard Stallman
Subject: Re: Instead of pcase
Date: Mon, 11 Dec 2023 22:43:44 -0500

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

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

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





reply via email to

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