Fork me on GitHub
#code-reviews
<
2018-04-27
>
tristefigure20:04:17

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)))

slipset20:04:37

Looks a lot like cond->>

tristefigure20:04:41

cond-> doesn’t handle threading the value to test forms

seancorfield20:04:16

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.

tristefigure20:04:35

I wrote a macro to define -> and ->> variants in one shot. With a few examples: