[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)
Re: Instead of pcase, Richard Stallman, 2023/12/01
Re: Instead of pcase, Lynn Winebarger, 2023/12/02
Re: Instead of pcase, Richard Stallman, 2023/12/10
- Re: Instead of pcase, Lynn Winebarger, 2023/12/10
- Re: Instead of pcase, Michael Heerdegen, 2023/12/10
- Re: Instead of pcase, Lynn Winebarger, 2023/12/10
- Re: Instead of pcase,
Richard Stallman <=
- Re: Instead of pcase, Tomas Hlavaty, 2023/12/12
- RE: [External] : Re: Instead of pcase, Drew Adams, 2023/12/12
- Re: [External] : Re: Instead of pcase, Richard Stallman, 2023/12/13
- RE: [External] : Re: Instead of pcase, Drew Adams, 2023/12/14
- Re: [External] : Re: Instead of pcase, Richard Stallman, 2023/12/15
- RE: [External] : Re: Instead of pcase, Drew Adams, 2023/12/16
- RE: [External] : Re: Instead of pcase, Bob Rogers, 2023/12/16
- Re: [External] : Re: Instead of pcase, Richard Stallman, 2023/12/17
Re: Instead of pcase, Tomas Hlavaty, 2023/12/12
RE: [External] : Re: Instead of pcase, Drew Adams, 2023/12/12