Skip to content
Snippets Groups Projects
Commit 0adc6fad authored by Cam Saül's avatar Cam Saül
Browse files

Merge pull request #1464 from metabase/cond-as->-macro

Need cond-as-> macro :flushed:
parents 0a1c07bf 8c0c7e4f
Branches
Tags
No related merge requests found
......@@ -18,6 +18,7 @@
(catch-api-exceptions 0)
(check 1)
(checkp 1)
(cond-as-> 2)
(cond-let 0)
(conda 0)
(context 2)
......
......@@ -293,4 +293,24 @@
(throw (Exception. (format "Timed out after %d milliseconds." ~timeout-ms))))
result#))
(defmacro cond-as->
"Anaphoric version of `cond->`. Binds EXPR to NAME through a series
of pairs of TEST and FORM. NAME is successively bound to the value
of each FORM whose TEST succeeds.
(defn maybe-wrap-fn [before after f]
(as-> f <>
(fn? before) (fn [] (before) (<>))
(fn? after) (fn [] (try (<>)
(finally (after))))))"
{:arglists '([expr nm tst form & more])}
[expr nm & clauses]
{:pre [(even? (count clauses))]}
`(let [~nm ~expr
~@(apply concat (for [[tst form] (partition 2 clauses)]
[nm `(if ~tst
~form
~nm)]))]
~nm))
(require-dox-in-this-namespace)
......@@ -81,3 +81,24 @@
(expect -7
((rpartial - 5 10) 8))
;;; ## cond-as->
(expect 100
(cond-as-> 100 <>))
(expect 106
(cond-as-> 100 <>
true (+ 1 <>)
false (+ 10 <>)
:ok (+ 5 <>)))
(expect 101
(cond-as-> 100 <>
(odd? <>) (inc <>)
(even? <>) (inc <>)))
(expect 102
(cond-as-> 100 <>
(even? <>) (inc <>)
(odd? <>) (inc <>)))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment