Skip to content

Instantly share code, notes, and snippets.

@bsless
Created April 4, 2020 14:39
Show Gist options
  • Save bsless/d0cf0cc2cbd01d3871d35a3356be21a7 to your computer and use it in GitHub Desktop.
Save bsless/d0cf0cc2cbd01d3871d35a3356be21a7 to your computer and use it in GitHub Desktop.
common lisp cond allows returning the predicate's value. This form does it via reader macros
(defmacro cond*
[& clauses]
(when clauses
(let [p (first clauses)
return? (:return (meta p))]
(if return?
`(if ~p
~p
(cond* ~@(next clauses)))
`(if ~p
~(if (next clauses)
(second clauses)
(throw (IllegalArgumentException.
"cond requires an even number of forms")))
(cond* ~@(nnext clauses)))))))
;;; examples:
(cond*
p0 c0
^:return p1
p2 c2
p3 c3)
(cond*
^:return p1
p2 c2
p3 c3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment