Created
February 12, 2012 15:27
-
-
Save sgr/1809056 to your computer and use it in GitHub Desktop.
condx: extends cond to allow 'if-let' style binding 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
(defmacro condx-aux [f & clauses] | |
(when clauses | |
(list f (first clauses) | |
(if (next clauses) (second clauses) | |
(throw (IllegalArgumentException. "condx requires an even number of forms"))) | |
(cons 'condx (next (next clauses)))))) | |
(defmacro condx | |
"condx extends cond to allow 'if-let' style binding forms. | |
Example: | |
(condx [x (re-matches #\"\\((.+)\\)\\s*\\[(.+)\\]\\s*(.+)\" s)] {:category (nth x 1) :author (nth x 2) :title (nth x 3)} | |
[x (re-matches #\"\\[(.+)\\]\\s*(.+)\" s)] {:author (nth x 1) :title (nth x 2)} | |
(= 0 (.length s)) "NO TITLE" | |
:else {:title s})" | |
[& clauses] | |
(when clauses | |
(if (vector? (first clauses)) | |
`(condx-aux if-let ~@clauses) | |
`(condx-aux if ~@clauses)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment