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 06:39:28 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Drew Adams <address@hidden> writes:

> We usually try to describe the args in order (first CLAUSES
> then THEN-FORM, then ELSE-FORMS.  So, for example:
>
>   Depending on CLAUSES, evaluate THEN-FORM or ELSE-FORMS.

Thanks, fixed.

> And why the asymmetry between THEN being singular
> and ELSE being plural?  Emacs-Lisp `if' has such an
> asymmetry (for a couple of reasons, which aren't
> particularly relevant here), but why should this
> `pcase-if' construct be asymmetric? Why not make it
> symmetric, like `cond' (and `case'), since it's
> already a complex, multipurpose critter?

I think if we don't make the syntax similar to if, while the semantics
is, people would become crazy.  If you want something more like `case',
well, use `pcase' ;-)

> This naming is no longer relevant, is it?  There's
> no set of "cases" involved.  Is there really a big
> benefit in naming everything that uses a "pcase-style"
> pattern "pcase-<SOMETHING>"?  Presumably you want to
> use similar names to convey the fact that they all
> do pattern-matching.

Yes, your are right with everything, the names are nonsense.  I guess we
are still in some kind of intermediate state - AFAIK Stefan hopes to
integrate the pcase stuff more into Emacs innards some day.  Dealing
with these wide problems is not what I want to do here.  For now, I just
want to be consistent with the existing naming scheme.


Regards,

Michael.


P.S.: Here is the updated definition:

#+begin_src emacs-lisp
(defmacro pcase-if (clauses then &rest elses)
  "Depending on CLAUSES, evaluate THEN or ELSES.
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, else the
ELSES."
  (declare (indent 2)
           (debug ((&rest (pcase-PAT &optional form))
                   form body)))
  (if (null clauses)
      then
    (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 ,@elses)))))))
#+end_src





reply via email to

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