Skip to content

Instantly share code, notes, and snippets.

@frenchy64
Created July 22, 2011 05:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frenchy64/1098930 to your computer and use it in GitHub Desktop.
Save frenchy64/1098930 to your computer and use it in GitHub Desktop.
;; (match [x y z]
;; [_ f# t#] 1
;; [f# t# _ ] 2
;; [_ _ f#] 3
;; [_ _ t#] 4)
(def pm2 (pattern-matrix [(pattern-row [wildcard (pattern false) (pattern true)] :a1)
(pattern-row [(pattern false) (pattern true) wildcard] :a2)
(pattern-row [wildcard wildcard (pattern false)] :a3)
(pattern-row [wildcard wildcard (pattern true)] :a4)]
'[x y z]))
(print-matrix pm2)
; | x y z |
; | _ f# t# | :a1
; | f# t# _ | :a2
; | _ _ f# | :a3
; | _ _ t# | :a4
(compile pm2)
;#match.core.SwitchNode[y, [[<Pattern: true>
; #match.core.SwitchNode[x, [[<Pattern: false>
; #match.core.LeafNode[:a2]]
; [default17534 #match.core.FailNode[]]]]]
; [<Pattern: false>
; #match.core.SwitchNode[z, [[<Pattern: true>
; #match.core.LeafNode[:a1]]
; [<Pattern: false> #match.core.LeafNode[:a3]]
; [default17535 #match.core.FailNode[]]]]]
; [default17536 #match.core.FailNode[]]]]
(to-clj (compile pm2))
; (clojure.core/cond
; (clojure.core/= y true) (clojure.core/cond
; (clojure.core/= x false) :a2
; (clojure.core/= x _) (throw (java.lang.Exception. "Found FailNode")))
; (clojure.core/= y false) (clojure.core/cond
; (clojure.core/= z true) :a1
; (clojure.core/= z false) :a3
; (clojure.core/= z _) (throw (java.lang.Exception. "Found FailNode")))
; (clojure.core/= y _) (throw (java.lang.Exception. "Found FailNode")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment