[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question on pcase
From: |
Michael Heerdegen |
Subject: |
Re: Question on pcase |
Date: |
Tue, 27 Oct 2015 15:27:30 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux) |
Oleh Krehel <address@hidden> 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.
- RE: Question on pcase, (continued)
- RE: Question on pcase, Drew Adams, 2015/10/24
- Re: Question on pcase, Alan Mackenzie, 2015/10/24
- RE: Question on pcase, Drew Adams, 2015/10/24
- Re: Question on pcase, Johan Bockgård, 2015/10/24
- Re: Question on pcase, Michael Heerdegen, 2015/10/24
- Re: Question on pcase, Oleh Krehel, 2015/10/26
- Re: Question on pcase, Michael Heerdegen, 2015/10/26
- Re: Question on pcase, Oleh Krehel, 2015/10/27
- Re: Question on pcase, Michael Heerdegen, 2015/10/26
- Re: Question on pcase, Oleh Krehel, 2015/10/27
- Re: Question on pcase,
Michael Heerdegen <=
- Re: Question on pcase, Michael Heerdegen, 2015/10/27
- Re: Question on pcase, Michael Heerdegen, 2015/10/28
- Re: Question on pcase, Oleh Krehel, 2015/10/29
- Re: Question on pcase, Michael Heerdegen, 2015/10/29
- Re: Question on pcase, Andreas Schwab, 2015/10/26