Skip to content
Bozhidar Batsov edited this page Nov 13, 2015 · 9 revisions

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 core.match 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 - IPersistentVector, ILookup, 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 4.

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.

See Basic Usage for more examples and Advanced Usage for directions on how to extend your custom data types so they can participate in pattern matching.