From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michael Heerdegen Newsgroups: gmane.emacs.devel Subject: Re: Question on pcase Date: Wed, 28 Oct 2015 19:05:13 +0100 Message-ID: <87fv0ukgx2.fsf@web.de> References: <871tcngdv2.fsf@gmail.com> <87k2qe1u09.fsf@web.de> <83r3kmrtat.fsf@gnu.org> <87r3kl22zk.fsf@web.de> <837fmdzpf2.fsf@gnu.org> <87oafp659p.fsf@web.de> <831tclzly9.fsf@gnu.org> <87fv115t20.fsf@web.de> <87io5tzkt8.fsf@gmail.com> <87fv0xd2jr.fsf@web.de> <87egggya6e.fsf@gmail.com> <87twpcidyl.fsf@web.de> <87h9lcid0g.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1446055569 31706 80.91.229.3 (28 Oct 2015 18:06:09 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 28 Oct 2015 18:06:09 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 28 19:05:55 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZrV6e-0000Lv-UK for ged-emacs-devel@m.gmane.org; Wed, 28 Oct 2015 19:05:53 +0100 Original-Received: from localhost ([::1]:39974 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrV6e-0005OF-Ap for ged-emacs-devel@m.gmane.org; Wed, 28 Oct 2015 14:05:52 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35181) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrV6G-0005O6-Vx for emacs-devel@gnu.org; Wed, 28 Oct 2015 14:05:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZrV6D-0005JO-P7 for emacs-devel@gnu.org; Wed, 28 Oct 2015 14:05:28 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:40466) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZrV6D-0005JE-Ho for emacs-devel@gnu.org; Wed, 28 Oct 2015 14:05:25 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1ZrV6A-0008Lr-Si for emacs-devel@gnu.org; Wed, 28 Oct 2015 19:05:22 +0100 Original-Received: from ip-90-186-3-138.web.vodafone.de ([90.186.3.138]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 28 Oct 2015 19:05:22 +0100 Original-Received: from michael_heerdegen by ip-90-186-3-138.web.vodafone.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 28 Oct 2015 19:05:22 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 109 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-90-186-3-138.web.vodafone.de User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) Cancel-Lock: sha1:oxz548Vwx/1JzvtWz6LDtG6PQMk= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:192807 Archived-At: Michael Heerdegen 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.