Skip to content

Instantly share code, notes, and snippets.

@ihodes
Last active January 1, 2016 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihodes/8120514 to your computer and use it in GitHub Desktop.
Save ihodes/8120514 to your computer and use it in GitHub Desktop.
(ns fun.matching
(:require [clojure.core.match :refer [match emit-pattern to-source groupable?]]))
;; adding set literals to match
(defrecord SetPattern [set])
(defmethod emit-pattern clojure.lang.PersistentHashSet
[pat]
(SetPattern. pat))
(defmethod groupable? [SetPattern SetPattern]
[a b]
(let [a (:set a)
b (:set b)]
(= a b)))
(defmethod to-source SetPattern
[pat ocr]
`(contains? ~(:set pat) ~ocr))
;; This allows
(match [[1 2 "green"]]
[[1 _ "blue"]] :a0
[[_ 2 #{"green" "blue"}]] :a1) ; => :a1
;; I'd like to add e.g.
(match [[1 2 "green"]]
[[1 _ #(= % "green")]] :a0
[[_ 2 #{"green" "blue"}]] :a1) ; => :a0
;; But the below won't work
(defrecord FunctionPattern [fn])
(defmethod emit-pattern clojure.lang.IFn
[pat]
(FunctionPattern. pat))
(defmethod to-source FunctionPattern
[pat ocr]
`(~(:fn pat) ~ocr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment