all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: Tino Calancha <tino.calancha@gmail.com>
Cc: Emacs developers <emacs-devel@gnu.org>
Subject: Re: Anaphoric macros: increase visibility
Date: Sun, 15 Jan 2017 03:26:23 +0100	[thread overview]
Message-ID: <878tqdqcs0.fsf@web.de> (raw)
In-Reply-To: <87k29xqhqp.fsf@web.de> (Michael Heerdegen's message of "Sun, 15 Jan 2017 01:39:10 +0100")

Michael Heerdegen <michael_heerdegen@web.de> writes:

> And I often think that it would be cool to couple them with pcase
> pattern matching.  OTOH we already have `pcase-let', but I can't get
> along with its semantics, e.g.
>
> (pcase-let ((`(,a ,b) '(1 2 3)))
>   (list a b))
>
> ==> (1 2)
>
> although the pattern doesn't match.

I think I would prefer something like

#+begin_src emacs-lisp
(defmacro pwhen (pairs &rest body)
  (declare (indent 1))
  `(pcase nil ((and ,@(mapcar (lambda (pair) (cons 'let pair)) pairs)) ,@body)))
#+end_src

This is similar to `when-let' but instead of a non-nil test, you can use
any test you want (via pattern matching).  There would not be an
implicit test for whether a matched expression evals to something
non-nil, however.

Example:

#+begin_src emacs-lisp
(defun test-pwhen (thing)
  (pwhen ((`(,x ,y)       thing)
          ((pred identity) x))
    (message "passed with x: %S and y:%S" x y)))
#+end_src

(test-pwhen 37)
   ==> nil
(test-pwhen '(1 2 3))
   ==> nil
(test-pwhen '(nil 32))
   ==> nil
(test-pwhen '(1 2))
   ==> "passed with x: 1 and y:2"


Michael.



  parent reply	other threads:[~2017-01-15  2:26 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-13  8:39 Anaphoric macros: increase visibility Tino Calancha
2017-01-13 19:48 ` Michael Heerdegen
2017-01-14  2:30   ` Rolf Ade
2017-01-14  2:48     ` Rolf Ade
2017-01-14  3:03     ` Noam Postavsky
2017-01-14  4:13       ` Richard Copley
2017-01-14  5:27   ` Tino Calancha
2017-01-15  0:39     ` Michael Heerdegen
2017-01-15  2:24       ` On the naming/behavior of {if, when}-let (was Re: Anaphoric macros: increase visibility) Mark Oteiza
2017-01-15  2:26       ` Michael Heerdegen [this message]
2017-01-15 10:24       ` Anaphoric macros: increase visibility Tino Calancha
2017-01-15 15:32         ` Stefan Monnier
2017-01-16  2:44           ` Tino Calancha
2017-01-16  3:59             ` Stefan Monnier
2017-01-14  6:25   ` Dmitri Paduchikh
2017-01-14  7:56     ` Tino Calancha
2017-01-14 10:15       ` Dmitri Paduchikh
2017-01-15  0:29         ` Michael Heerdegen
2017-01-15  2:03           ` Dmitri Paduchikh
2017-01-15  2:16             ` 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=878tqdqcs0.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=emacs-devel@gnu.org \
    --cc=tino.calancha@gmail.com \
    /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.