emacs-devel
[Top][All Lists]
Advanced

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

pcase defuns


From: Andrew Hyatt
Subject: pcase defuns
Date: Sat, 18 Dec 2021 23:53:10 -0500


Hi all,

As a part of a personal project, I wrote a way to define functions in an equivalent way to pcases. For example:

(pcase-defun mytest (a b _)
 "Match on 'a 'b with the third argument a wildcard"
 "a b match")

(pcase-defun mytest (c ,var _)
 "Match on 'c binding VAR, with the third argument a wildcard"
 (format "c %s match" var) )

(mytest 'a 'b 'c) -> "a b match"
(mytest 'c 100 'c) -> "c 100 match"

This is all accomplished by a few small but tricky macros and a hashtable that holds all the rules.

I find this method of programming quite useful for certain kinds of problems, and pcase is close to what I want, but using pcase forced me to encapsulate all of my pattern matching logic in one function. Being able to have it spread out in pattern matching functions seems much nicer to me.

If this is of interest, I can either add this to the pcase module itself, create a package in gnu elpa, or (if there's some reason that it shouldn't be done) just keep it in a personal repository.

I'll wait for some guidance before creating a final version, with tests, wherever it should go.



reply via email to

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