all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Emacs Development <emacs-devel@gnu.org>
Subject: pcase-if-let?
Date: Thu, 29 Mar 2018 01:20:50 +0200	[thread overview]
Message-ID: <871sg3eqi5.fsf@web.de> (raw)

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.



             reply	other threads:[~2018-03-28 23:20 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-28 23:20 Michael Heerdegen [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871sg3eqi5.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.