unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* pcase-if-let?
@ 2018-03-28 23:20 Michael Heerdegen
  2018-03-28 23:54 ` pcase-if-let? Davis Herring
  0 siblings, 1 reply; 17+ messages in thread
From: Michael Heerdegen @ 2018-03-28 23:20 UTC (permalink / raw)
  To: Emacs Development

Hello,

should we add a `pcase-if-let' to pcase.el?  While the name might sound
a bit obscure, in my experience I quite often wanted something like
this, instead of having to write

#+begin_src emacs-lisp
(pcase value
  (pattern code
           more code
           ...)
  (_       other code
           more other code
           ...))
#+end_src

for example.

Here is the result of a first naive implementation trial (which works
for me):

#+begin_src emacs-lisp
(defmacro pcase-if-let (bindings then &rest elses)
  "Bind variables according to BINDINGS and eval THEN or ELSE.
This is like `if-let' but BINDINGS is a list of elements of the
form \(PATTERN VALUE-FORM) and it's tested whether all the
PATTERNs match instead of whether VALUE-FORMS are all non-nil."
  (declare (indent 2)
           (debug ((&rest (pcase-PAT &optional form))
                   form body)))
  (if (null bindings)
      '(let* () then)
    (let ((sym (make-symbol "sym"))
          (sucess-syms '())
          (last-sucess-sym nil))
      (dotimes (i (length bindings))
        (push (make-symbol (format "binding-%d-sucess" i)) sucess-syms))
      (cl-callf nreverse sucess-syms)
      `(progn
         (mapc (lambda (,sym) (setq ,sym nil)) ',sucess-syms)
         (pcase nil
           ((and ,@(mapcar (pcase-lambda (`(,pattern ,value))
                             `(let (and ,@(if last-sucess-sym `((guard ,last-sucess-sym)) '())
                                        ,pattern
                                        (let ,(setq last-sucess-sym (pop sucess-syms)) t))
                                ,value))
                           bindings))
            (if ,last-sucess-sym ,then ,@elses)))))))
#+end_src

Note that there is no need for a `pcase-if' (just don't bind variables
in the patterns), so we could also use this name I think.  Also note
that we can't unite this macro with `if-let' (because it is about
non-nil-ness of values, while `pcase-if-let' is about pattern matching,
so a binding like (SYMBOL VALUE) has different semantics).


Thanks,

Michael.



^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2018-04-17 21:04 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-28 23:20 pcase-if-let? Michael Heerdegen
2018-03-28 23:54 ` pcase-if-let? Davis Herring
2018-03-29  0:21   ` pcase-if-let? Michael Heerdegen
2018-03-29  3:46     ` pcase-if-let? Michael Heerdegen
2018-03-29  4:14       ` pcase-if-let? Drew Adams
2018-03-29  4:39         ` pcase-if-let? Michael Heerdegen
2018-03-29  4:49           ` pcase-if-let? Drew Adams
2018-03-29  4:53           ` pcase-if-let? Stefan Monnier
2018-03-29  5:24             ` pcase-if-let? Michael Heerdegen
2018-03-29 11:59               ` pcase-if-let? Stefan Monnier
2018-03-30  1:52                 ` pcase-if-let? Michael Heerdegen
2018-03-30  4:07                   ` pcase-if-let? Drew Adams
2018-03-30  5:09                     ` pcase-if-let? Michael Heerdegen
2018-03-30 15:07                       ` pcase-if-let? Drew Adams
2018-03-30 23:22                         ` pcase-if-let? Michael Heerdegen
2018-04-17 20:26           ` pcase-if-let? Nathan Moreau
2018-04-17 21:04             ` pcase-if-let? Michael Heerdegen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).