diff options
-rw-r--r-- | share/txr/stdlib/match.tl | 9 | ||||
-rw-r--r-- | txr.1 | 38 |
2 files changed, 34 insertions, 13 deletions
diff --git a/share/txr/stdlib/match.tl b/share/txr/stdlib/match.tl index d1c45559..00918f37 100644 --- a/share/txr/stdlib/match.tl +++ b/share/txr/stdlib/match.tl @@ -194,6 +194,14 @@ (set var-match.test-expr ^(and ,var-match.test-expr [,op-expr ,obj-var])) var-match)) +(defun compile-dwim-predicate-match (pred-expr obj-var var-list) + (mac-param-bind *match-form* (dwim fun : sym) pred-expr + (or (null sym) (bindable sym) + (compile-error *match-form* "~s is not a symbol" sym)) + (let ((var-match (compile-var-match sym obj-var var-list))) + (set var-match.test-expr ^(and ,var-match.test-expr [,fun ,obj-var])) + var-match))) + (defun compile-predicate-match (pred-expr obj-var var-list) (mac-param-bind *match-form* (fun : sym) pred-expr (or (null sym) (bindable sym) @@ -426,6 +434,7 @@ (op (compile-op-match exp obj-var var-list)) (hash (compile-hash-match exp obj-var var-list)) (rcons (compile-range-match exp obj-var var-list)) + (dwim (compile-dwim-predicate-match exp obj-var var-list)) (t (compile-predicate-match exp obj-var var-list))) (compile-error *match-form* "unrecognized pattern syntax ~s" pat)))) @@ -40520,11 +40520,12 @@ function is capable of being called with exactly one argument. .NP* Pattern predicate operator .synb .mets >> @( function <> [ symbol ]) +.mets >> @[ expression <> [ symbol ]] .syne .desc -Whenever the operator of a pattern predicate is not recognized, that operator -is assumed to be the name of a function, and the syntax is treated as the -predicate operator. +Whenever the operator of a pattern predicate is not recognized and is +a bindable symbol, that operator is assumed to be the name of a function, and +the syntax is treated as the predicate operator. The pattern predicate operator invokes the function, passing it the corresponding object as an argument. The match is successful if the function @@ -40547,24 +40548,35 @@ The .meta function must be capable of accepting exactly one argument. +The +.mono +.meti >> @[ expression <> [ symbol ]] +.onom +version of the syntax is always a predicate. It denotes the evaluation of +.mono +.meti >> [ expression << arg ] +.onom +where +.meta arg +is the corresponding object. + Note: a .code lambda -expression may be used as -.metn function . +expression may be used as either +.meta function +or +.metn expression . .TP* Example: .verb (when-match (@(evenp) @(oddp x)) '(2 3) x) -> 3 -.brev -An instance of the -.mono -.meti >> @( operator ...) -.onom -syntax denotes the predicate operator whenever the -.meta operator -symbol is not one of the predicates. + ;; use hash table as predicate: + (let ((h #H(() (a 1) (b 2)))) + (when-match @[h x] 'a x)) + -> a +.brev .coNP Macros @ when-match and @ if-match .synb |