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: Tue, 27 Oct 2015 15:27:30 +0100 Message-ID: <87twpcidyl.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> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1445956139 19816 80.91.229.3 (27 Oct 2015 14:28:59 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 27 Oct 2015 14:28:59 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Oct 27 15:28:51 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 1Zr5F3-0005xr-Cr for ged-emacs-devel@m.gmane.org; Tue, 27 Oct 2015 15:28:49 +0100 Original-Received: from localhost ([::1]:59987 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr5F2-00053U-QX for ged-emacs-devel@m.gmane.org; Tue, 27 Oct 2015 10:28:48 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53974) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr5EB-0004Ng-PP for emacs-devel@gnu.org; Tue, 27 Oct 2015 10:28:01 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zr5E2-0005gl-4W for emacs-devel@gnu.org; Tue, 27 Oct 2015 10:27:55 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:53035) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zr5E1-0005gT-Tl for emacs-devel@gnu.org; Tue, 27 Oct 2015 10:27:46 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Zr5Dz-0005Ei-7o for emacs-devel@gnu.org; Tue, 27 Oct 2015 15:27:43 +0100 Original-Received: from ip-90-186-3-87.web.vodafone.de ([90.186.3.87]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 27 Oct 2015 15:27:43 +0100 Original-Received: from michael_heerdegen by ip-90-186-3-87.web.vodafone.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 27 Oct 2015 15:27:43 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 69 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-90-186-3-87.web.vodafone.de User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) Cancel-Lock: sha1:S9CNoALOniHe8ihwGa4AJCuaU4k= 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:192726 Archived-At: Oleh Krehel writes: > I debug with lispy [1], my package for Elisp and stuff. It's just a > sophisticated "C-x C-e" - all the program stack is in the global > variables. Ah, in global variables. Now I understand why you wanted to use `setq'. Hmm, as I said before, it's impossible in the general case to separate a `pcase' pattern into a side effect free matching part and a binding/setting part. That's because when variable binding is done, bindings established while performing the matching need to be in effect; look at the example at the end to see what I mean. So, the best I can offer is a function that transforms a PATTERN into a function F that takes one argument EXP, the expression to be matched. F returns non-nil when the PATTERN would match EXP, else nil. When the PATTERN matches, all bindings created as a side effect from matching that could be referred to in the CODE part of a (PATTERN CODE) `pcase' branch are set with `setq'. --8<---------------cut here---------------start------------->8--- ;; -*- lexical-binding: t -*- (defun abo-transform-pcase-pattern (pattern) (let ((arg (make-symbol "expression"))) `(lambda (,arg) ,(pcase--u `((,(pcase--match arg (pcase--macroexpand pattern)) ,(lambda (vars) `(progn ,@(mapcar (lambda (b) `(setq ,(car b) ,(cdr b))) vars))))))))) --8<---------------cut here---------------end--------------->8--- Because this is a function, you need to quote the PATTERN. Example: transform the pattern `(,a ,b) (abo-transform-pcase-pattern '`(,a ,b)) ==> (lambda (#1=#:expression) (if (consp #1#) (let* ((#5=#:x (car #1#)) (#2=#:x (cdr #1#))) (if (consp #2#) (let* ((#4=#:x (car #2#)) (#3=#:x (cdr #2#))) (if (null #3#) (progn (setq b #4#) (setq a #5#)) nil)) nil)) nil)) Regards, Michael.