m2ym / optima

Optimized Pattern Matching Library for Common Lisp
268 stars 19 forks source link

Proof of concept: accessor-pattern #90

Closed guicho271828 closed 9 years ago

guicho271828 commented 10 years ago

15-minutes hack, but it works. If we further expand this strategy, almost all constructor patterns can be derived from the combination of this one and other primitive patterns e.g. type/and/guard patterns.

Note: of course you don't have to merge this commit.

guicho271828 commented 10 years ago

For example, ppcre-pattern can be implemented as follows:

(defpattern ppcre (regex &rest subpatterns)
   `(and (type string)
           (access #'(lambda (x)
                            (nth-value 1 (ppcre:scan-to-strings ,regex x)))
                        (and (vector ,@subpatterns))))
guicho271828 commented 10 years ago

pros: we don't have to implement/export the new/internal API.

cons: compiler efficiency?

guicho271828 commented 10 years ago

Reading Fabrice et. al. It seems like the "constructor" is one of the keys in the algorithm, so instead of just 'accessor', "constructor" is what should be exported.