Skip to content
David Nolen edited this page Jun 17, 2013 · 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))

We've formatted it to make the clauses a bit more clear. Obviously this expression will return 4.

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

Clone this wiki locally