Here's what I ended up with - minor variants of cond-> and cond->>
(defmacro condp->
"Takes an expression and a set of predicate/form pairs. Threads expr (via ->)
through each form for which the corresponding predicate is true of expr.
Note that, unlike cond branching, condp-> threading does not short circuit
after the first true test expression."
[expr & clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[pred step]] `(if (~pred ~g) (-> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
~g)))
(defmacro condp->>
"Takes an expression and a set of predicate/form pairs. Threads expr (via ->>)
through each form for which the corresponding predicate is true of expr.
Note that, unlike cond branching, condp->> threading does not short circuit
after the first true test expression."
[expr & clauses]
(assert (even? (count clauses)))
(let [g (gensym)
pstep (fn [[pred step]] `(if (~pred ~g) (->> ~g ~step) ~g))]
`(let [~g ~expr
~@(interleave (repeat g) (map pstep (partition 2 clauses)))]
~g)))
On Feb 12, 2014, at 2:34 PM, Sean Corfield <[email protected]> wrote:
> On Feb 12, 2014, at 1:34 AM, Alex Baranosky <[email protected]>
> wrote:
>> I wrote pred-cond for Midje way back, which does what you want.
>> https://github.com/marick/Midje/blob/master/src/midje/clojure/core.clj#L176
>
> That doesn't appear to thread each expression through the "results" so it
> isn't really a variant of cond-> but between that and the source of cond-> I
> suspect I will just end up rolling my own...
signature.asc
Description: Message signed with OpenPGP using GPGMail
