Skip to content

Instantly share code, notes, and snippets.

@raek
Created June 21, 2010 12:07
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 raek/446760 to your computer and use it in GitHub Desktop.
Save raek/446760 to your computer and use it in GitHub Desktop.
(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