From: Michael Heerdegen <michael_heerdegen@web.de>
To: emacs-devel@gnu.org
Subject: Re: Question on pcase
Date: Wed, 28 Oct 2015 19:05:13 +0100 [thread overview]
Message-ID: <87fv0ukgx2.fsf@web.de> (raw)
In-Reply-To: 87h9lcid0g.fsf@web.de
Michael Heerdegen <michael_heerdegen@web.de> writes:
> But AFAIU `abo-transform-pcase-pattern' should be sufficient for your
> purpose.
Here is a version that should fit your use case better:
--8<---------------cut here---------------start------------->8---
;; -*- lexical-binding: t -*-
(defun abo-abo-pattern-matcher (pattern)
"Turn pcase PATTERN into a predicate.
For any given pcase PATTERN, return a predicate P that returns
non-nil for any EXP when and only when PATTERN matches EXP. In
that case, P returns a list of the form (bindings . BINDINGS) as
non-nil value, where BINDINGS is a list of bindings that pattern
matching with PATTERN would actually establish in a pcase branch."
(let ((arg (make-symbol "exp")))
`(lambda (,arg)
,(pcase--u
`((,(pcase--match arg (pcase--macroexpand pattern))
,(lambda (vars)
`(cons
'bindings
(list
,@(nreverse (mapcar
(lambda (binding)
`(cons ',(car binding)
,(cdr binding)))
vars)))))))))))
--8<---------------cut here---------------end--------------->8---
Example: Create a matcher predicate for the pattern
`(,(and (pred integerp) x)
,(and (pred integerp)
(pred (< 0))
y))
and bind it to the variable `matcher':
(setq matcher
(abo-abo-pattern-matcher
'`(,(and (pred integerp) x)
,(and (pred integerp)
(pred (< 0))
y))))
==>
--8<---------------cut here---------------start------------->8---
(lambda
(#1=#:exp)
(if
(consp #1#)
(let*
((#2=#:x
(car #1#)))
(if
(integerp #2#)
(let*
((#3=#:x
(cdr #1#)))
(if
(consp #3#)
(let*
((#4=#:x
(car #3#)))
(cond
((not
(integerp #4#))
nil)
((< 0 #4#)
(let*
((#5=#:x
(cdr #3#)))
(if
(null #5#)
(cons 'bindings
(list
(cons 'x #2#)
(cons 'y #4#)))
nil)))
(t nil)))
nil))
nil))
nil))
--8<---------------cut here---------------end--------------->8---
(funcall matcher '(1 0))
==> nil
(funcall matcher '(1 2))
==> (bindings
(x . 1)
(y . 2))
Regards,
Michael.
next prev parent reply other threads:[~2015-10-28 18:05 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-22 14:46 Question on pcase Oleh Krehel
2015-10-22 21:19 ` Michael Heerdegen
2015-10-23 6:30 ` Eli Zaretskii
2015-10-23 11:58 ` Oleh Krehel
2015-10-23 12:23 ` Michael Heerdegen
2015-10-23 12:42 ` Oleh Krehel
2015-10-23 13:07 ` Michael Heerdegen
2015-10-23 13:26 ` Oleh Krehel
2015-10-23 13:59 ` Michael Heerdegen
2015-10-27 23:50 ` Johan Bockgård
2015-10-30 1:33 ` Michael Heerdegen
2015-10-23 14:23 ` Michael Heerdegen
2015-10-23 12:17 ` Michael Heerdegen
2015-10-23 12:22 ` Oleh Krehel
2015-10-23 13:26 ` Eli Zaretskii
2015-10-23 14:14 ` Michael Heerdegen
2015-10-23 14:41 ` Eli Zaretskii
2015-10-23 18:38 ` Michael Heerdegen
2015-10-23 18:43 ` Eli Zaretskii
2015-10-23 19:59 ` Przemysław Wojnowski
2015-10-23 21:01 ` Michael Heerdegen
2015-10-23 20:23 ` Przemysław Wojnowski
2015-10-23 20:39 ` Michael Heerdegen
2015-10-24 11:37 ` Przemysław Wojnowski
2015-10-24 9:01 ` Alan Mackenzie
2015-10-24 12:58 ` Stephen Berman
2015-10-24 17:47 ` Alan Mackenzie
2015-10-24 19:10 ` Stephen Berman
2015-10-24 19:28 ` Alan Mackenzie
2015-10-25 0:00 ` pcase docstring tweaks (was: Question on pcase) Michael Heerdegen
2015-10-27 14:54 ` pcase docstring tweaks Michael Heerdegen
2015-10-27 18:57 ` Stefan Monnier
2015-10-27 20:14 ` Michael Heerdegen
2015-10-28 3:15 ` Richard Stallman
2015-10-28 17:08 ` Michael Heerdegen
2015-10-24 17:00 ` Question on pcase Drew Adams
2015-10-24 17:22 ` Alan Mackenzie
2015-10-24 17:36 ` Drew Adams
2015-10-24 20:03 ` Johan Bockgård
2015-10-24 23:11 ` Michael Heerdegen
2015-10-26 15:55 ` Oleh Krehel
2015-10-26 16:07 ` Michael Heerdegen
2015-10-27 8:42 ` Oleh Krehel
2015-10-26 16:20 ` Michael Heerdegen
2015-10-27 8:42 ` Oleh Krehel
2015-10-27 14:27 ` Michael Heerdegen
2015-10-27 14:47 ` Michael Heerdegen
2015-10-28 18:05 ` Michael Heerdegen [this message]
2015-10-29 9:44 ` Oleh Krehel
2015-10-30 1:11 ` Michael Heerdegen
2015-10-26 16:35 ` Andreas Schwab
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87fv0ukgx2.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 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).