-
-
Save MayDaniel/947020 to your computer and use it in GitHub Desktop.
seqex - a tiny pattern matcher in Clojure
This file contains hidden or 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 seqex.match) | |
| (defn cps [funct cont] | |
| (fn [[f & r :as s]] | |
| (if (funct f) | |
| (cont r) | |
| false))) | |
| (defn literal [l cont] | |
| (cps (partial = l) cont)) | |
| (defn _ [cont] | |
| (cps (constantly true) cont)) | |
| (defn other [funct _] funct) | |
| (defmacro match | |
| ([form] | |
| (if (seq? form) | |
| `(~@form nil?) | |
| `(~form nil?))) | |
| ([form & forms] | |
| (if (seq? form) | |
| `(~@form (match ~@forms)) | |
| `(~form (match ~@forms))))) | |
| (defmacro coll [& forms] | |
| `(cps #(and (coll? %) | |
| ((match ~@(drop-last forms)) %)) | |
| ~(last forms))) |
This file contains hidden or 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
| ((match | |
| (literal 1) | |
| (coll (literal 3) | |
| (other #(every? even? %))) | |
| _ | |
| (literal 3)) | |
| [1 [3 4 2] 2 3]) | |
| ; true | |
| ((literal 1 (literal 2 (literal 3 nil?))) [1 2 3]) | |
| ; true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment