Priority in core.match patterns

227 views Asked by At

In my core.match patterns I want one specific pattern to have a "high priority". For example in:

(match 
[paragraph line     text     is-special ]
[_      _   #"^\s*$" _      ] "empty"       
[0      0   _    false      ] "project-header"
[0      _   _    false      ] "project-header-note"
[1      _   _    false      ] "next-action"
[2      _   _    false      ] "following-action"
:else "generic"))

I want the text variable in the "empty" row to always stop subsequent matching if it matches and return "empty". However probably because this clause is more generic it tries it as the last one and matches another one instead.

2

There are 2 answers

1
amalloy On

As far as I know, core.match doesn't support regexes as match variables in any "special" way. That is, your first clause might match if text were actually a regex, but if it is a string matching a regex, it will fail.

1
Michał Marczyk On

core.match's support for regexes lives in a separate namespace, clojure.core.match.regex. You'll need to require it before your function gets compiled for it to work the way you'd expect. The simplest, easiest and least error-prone way to do it is to add an appropriate :require clause to the ns clause of the namespace that you define your function in.

Also, pattern matching always tries the clauses in order. If you get a return value from a different clause than you expected, that means that that clause in fact failed to match your argument.