Created
June 21, 2010 12:07
-
-
Save raek/446760 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
(defn- re-clause-to-if [s [re bindings body] else] | |
`(if-let [[~'_ ~@bindings] (re-find ~re ~s)] | |
~body | |
~else)) | |
(defn- re-clauses-to-ifs [s clauses] | |
(when-first [clause clauses] | |
(if (and (not (next clauses)) | |
(= (count clause) 1)) | |
(first clause) | |
(re-clause-to-if s clause (re-clauses-to-ifs s (rest clauses)))))) | |
(defmacro cond-re [s & clauses] | |
(re-clauses-to-ifs s (partition 3 3 nil clauses))) | |
(comment ;example | |
(cond-re input-string | |
#"([0-9]+)" [n] (format "got a number: %d" n) | |
#"([0-9]+)/([0-9]+)" [num den] (format "got a ration: %d/%d" dum den) | |
#"\"(.*)\"" [s] (format "got a string: %s" s)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment