I just figured out something interesting and wanted to share it. Suppose you have a threading macro variant that accepts multiple inputs. Let’s suppose you have when->
in your toolbox.:
(-> 123
(when-> number? inc)
str)
; => "124"
(-> :x
(when-> number? inc)
str)
; => ":x"
Now suppose you want to rewire the inputs and output of when->
to specific arbitrary values. How would you write this ?
(-> 123
(when-> (<- *option*)
inc)
str)
(-> 123
(when-> number? (<- "number"))
(when-> keyword? name)
...)
Here is how I wrote <-
:
(defmacro <- [& body]
`((fn [& _#] ~@body)))
Looks a lot like cond->>
cond->
doesn’t handle threading the value to test forms
We have condp->
and condp->>
macros at work that thread through both the predicate and the expression. I suspect it's a fairly common pattern.
I wrote a macro to define ->
and ->>
variants in one shot. With a few examples: