emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase-if-let?


From: Michael Heerdegen
Subject: Re: pcase-if-let?
Date: Thu, 29 Mar 2018 05:46:49 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Michael Heerdegen <address@hidden> writes:

> Doesn't the empty `let*' create a new lexical environment?

Hmm, no, we thought that it should, but I think it doesn't, so I've
removed that `let*' wrapper for now.  And fixed some embarrassing
mistakes.  New version (named `pcase-if' this time):

#+begin_src emacs-lisp
(defmacro pcase-if (clauses then-form &rest else-forms)
  "Eval THEN-FORM or the ELSE-FORMS depending on CLAUSES.
CLAUSES is a list of the form \((PATTERN VALUE-FORM) ...)
Successively try to match every `pcase' PATTERN against its
VALUE-FORM.  When all match, eval THEN-FORM, else the
ELSE-FORMS."
  (declare (indent 2)
           (debug ((&rest (pcase-PAT &optional form))
                   form body)))
  (if (null clauses)
      then-form
    (let ((success-syms '()) (last-success-sym nil))
      (dotimes (i (length clauses))
        (push (make-symbol (format "matching-%d-success" i)) success-syms))
      (cl-callf nreverse success-syms)
      `(let ,(mapcar (lambda (s) `(,s nil)) success-syms)
         (pcase nil
           ((and ,@(mapcar (pcase-lambda (`(,pattern ,value))
                             `(let (and ,@(and last-success-sym `((guard 
,last-success-sym)))
                                        ,pattern
                                        (let ,(setq last-success-sym (pop 
success-syms)) t))
                                ,value))
                           clauses))
            (if ,last-success-sym ,then-form ,@else-forms)))))))
#+end_src

(The constructed pcase form is a bit unorthodox, but it was the simplest
thing that occurred to me.)

Michael.



reply via email to

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