See also

case-match ecase-match if-match when-match


Inspired by the Common Lisp library optima.

The simplest pattern is a constant, such as a number, string or 'expression. It matches anything that is equal to it.

user > (if-match 123 (+ 100 23) true)
it: true
user > (var expr '(1 2 3))
it: expr
user > (if-match '(1 2 3) expr true)
it: true

The symbol pattern is usually found as a subpattern of a structural pattern. The symbol names a variable that will be declared and bound when the pattern matches.

user > (var expr '(1 2 3))
it: expr
user > (if-match (list a b c) expr (print-line "a=${a} b=${b} c=${c}"))
a=1 b=2 c=3
it: null
user > (if-match (list 1 2 c) expr (print-line "c=${c}"))
it: null

Patterns can be combined with and, or and not.

user > (if-match (and (satisfies integer?) (satisfies odd?)) "a" true)
it: null
user > (if-match (and (satisfies integer?) (satisfies odd?)) 123 true)
it: true
user > (if-match (and (satisfies integer?) (satisfies odd?) x) 123 x)
it: 123

The property pattern tests one or more properties of a prototype object.

user > (var obj (new :name "teun" :city "leiden" :country "netherlands"))
it: obj
user > (if-match (property :name "teun" :city "leiden") obj true)
it: true
user > (if-match (property :name (type 'string)) obj true)
it: true
user > (if-match (property :city (regex "lei") name) obj name)
it: "leiden"



(and pattern*)
(or pattern*)
(not pattern)

(satisfies predicate-func)

(cons left-pattern right-pattern)
(vector pattern*)
(list pattern*)
(sequence pattern*)
(enumerable pattern*)

(quote expr)
(eq value)
(eql value)
(equal value)
(= value)
(equal-ci value)

(type type-name)
(regex regex-expr)
(property property-pattern*)