Deftype and defrecord matching

ZTO edited this page Mar 21, 2015 · 4 revisions

Being able to match directly on deftype and defrecords seems useful. We have a lot more information than we do in the case of primitive Java arrays - we have type information and via reflection we can get the field names, deftype and defrecord both have positional constructors.

(defn balance [node]
  (match [node]
    [(:or (Black. (Red. (Red. a x b) y c) z d)
          (Black. (Red. a x (Red. b y c)) z d)
          (Black. a x (Red. (Red. b y c) z d))
          (Black. a x (Red. b y (Red. c z d))))] :balance
    :else :balanced))