I have a clojure function that needs to push information into a map if a particular condition is true, using that map as a parameter for another function.
I have the following, but it feels clumsy with the repeated calls to the bar function.
(defn foo ([opts]
(if (= true (something))
(bar (into opts {:a b}))
(bar opts)))
(def bar [opts])
So if (something) is true, we push extra options into the opts parameter before calling the bar function, otherwise we just pass it through.
First thing is that
(= true (something))can be replaced simply by(something)with no issues (unless you are actually trying to differentiate between a return value oftrueand a return value of, say,1). If the options for the return value aretrueandfalse,(something)by itself will work fine. You can also use merge instead of into, which might be slightly clearer.You could try
This would work as well, though it involves calling
mergeunnecessarily when(something)is false, though withnilfor the second argument,mergeshould return very quickly.