emacs-devel
[Top][All Lists]
Advanced

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

Re: Question on pcase


From: Oleh Krehel
Subject: Re: Question on pcase
Date: Thu, 29 Oct 2015 10:44:14 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Michael Heerdegen <address@hidden> writes:

> Here is a version that should fit your use case better:
>
> ;; -*- 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)))))))))))

Thanks a lot Michael!

This seems to be exactly what I want. It even works for the initial
example of debugging `completion-at-point' that I cited earlier:

(funcall
 (abo-abo-pattern-matcher
  '`(,hookfun . (,start ,end ,collection . ,plist)))
 res)
;; =>
;; (bindings (hookfun . elisp-completion-at-point)
;;           (start . 1841)
;;           (end . 1844)
;;           (collection . [...])
;;           (plist
;;            :predicate fboundp :company-doc-buffer elisp--company-doc-buffer
;;            :company-docsig elisp--company-doc-string
;;            :company-location elisp--company-location))

Now I can add the `pcase' debug functionality to lispy.  I don't know if
there's a place for this type of a helper function in the core. I think
that we should at least encourage `pcase' to macroexpand to a simple
`cond' with a regular structure every time.

regards,
Oleh



reply via email to

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