Better way to nest if-let in clojure
clojure, if-statement
Solution
I think a macro may be necessary here to create the behavior you want. I have never written one (yet) but the following representation suggests to me that this might be fairly straightforward:
(let [{:keys [a b]} m]
(when (every? identity [a b])
(println (str "Processing " a " and " b))))
Using the `:keys` form of destructuring binding and `every?` enables a single specification of a vector of keys to destructure and check, and the bound locals are available in a following code block.
This could be used to make a macro such as `(when-every? [keys coll] code-with-bindings)`
I may update this answer with the macro code if I can take the time to work out how to do it.
Problem
Say I have a map of this form: ``` (def m {:a "A" :b "B"}) ``` and I want to do something if `:a` and `:b` are both not nil, I can do: ``` (if-let [a (:a m)] (if-let [b (:b m)] ... etc )) ``` or ``` (if (and (:a m) (:b m)) (let [{a :a b :b} m] ... etc )) ``` or even ``` (if (every? m [:a :b]) (let [{a :a b :b} m] ... etc )) ``` Is there a neater (ie one-line) way to achieve this?