emacs-devel
[Top][All Lists]
Advanced

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

Re: Anaphoric macros: increase visibility


From: Michael Heerdegen
Subject: Re: Anaphoric macros: increase visibility
Date: Sun, 15 Jan 2017 03:26:23 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1.91 (gnu/linux)

Michael Heerdegen <address@hidden> 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.



reply via email to

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