Clone this wiki locally
core.match adds sophisticated pattern matching support to the Clojure
programming language. Currently two implementations are fully
supported - Clojure on the JVM, and ClojureScript. Because
is implemented purely via macros it should be trivial to get up
and running on other Clojure implementations.
Unlike pattern matching found in most functional programming languages
core.match does not promote matching on concrete types. Instead
core.match matches on abstract interfaces / protocols -
Sequential and so on.
Here is a simple pattern match not much different from what you might find in popular functional programming languages.
(let [x true y true z true] (match [x y z] [_ false true ] 1 [false true _ ] 2 [_ _ false] 3 [_ _ true ] 4 :else 5)) ;=> 4
We've formatted it to make the clauses a bit more clear. Obviously
this expression will return
core.match supports matching sequential types, vectors types, map
types, and POJOs. It supports matching on local variables, and
features both or patterns and guard patterns. It also has experimental
support for matching on concrete Java arrays as well as binary data.