I am interested in extending pcase
with patterns which:
- Complement an existing pattern, e.g.
(not (pred string-empty-p))
. Such a pattern would match where the non-negated form does not match and vice versa. - Match when a predicate applied to the object being matched returns
nil
, e.g.(npred (string= "foo"))
.
The following is my first attempt at the former:
(pcase-defmacro not (pat)
(let ((obj (make-symbol "obj")))
`(and ,obj (guard (not (pcase ,obj (,pat)))))))
I would appreciate any suggestions for improvement of (1) (e.g. how to avoid a nested pcase
) and implementation approaches for (2). Are there any conditions under which such patterns would be a bad idea (other than there being a better, non-pcase
alternative)? Is there an idiomatic/elegant way of achieving the same effect which does not involve defining new pcase
patterns?
Update
I think the pred
pattern is constrained enough to allow for the following implementation for (2):
(eval-when-compile (require 'subr-x))
(pcase-defmacro npred (pat)
(let ((obj (make-symbol "obj")))
`(and ,obj (guard (not (thread-last ,obj ,pat))))))