Skip to content

Instantly share code, notes, and snippets.

@sgr
Created February 12, 2012 15:27
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 sgr/1809056 to your computer and use it in GitHub Desktop.
Save sgr/1809056 to your computer and use it in GitHub Desktop.
condx: extends cond to allow 'if-let' style binding forms.
(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