Last active
January 1, 2016 08:49
-
-
Save ihodes/8120514 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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