Pattern Matching with core.match

Other topics

Remarks:

The core.match library implements a pattern match compilation algorithm that uses the notion of "necessity" from lazy pattern matching.

Matching Literals

(let [x true
      y true
      z true]
  (match [x y z]
     [_ false true] 1
     [false true _ ] 2
     [_ _ false] 3
     [_ _ true] 4))

;=> 4

Matching a Vector

(let [v [1 2 3]]
  (match [v]
    [[1 1 1]] :a0
    [[1 _ 1]] :a1
    [[1 2 _]] :a2))  ;; _ is used for wildcard matching

;=> :a2

Matching a Map

(let [x {:a 1 :b 1}]
   (match [x]
     [{:a _ :b 2}] :a0
     [{:a 1 :b _}] :a1
     [{:x 3 :y _ :z 4}] :a2))

;=> :a1

Matching a literal symbol

(match [['asymbol]]
  [['asymbol]] :success)

;=> :success

Contributors

Topic Id: 2569

Example Ids: 8502,8503,8504,8505

This site is not affiliated with any of the contributors.