I have a token scanner that simply returns nil for characters I'm not interested in. Rather than conj the nils to my token vector and then later stripping them all out, I want to simply not add them.
I'm using
;; dont conjoin if value false
(defn condj [v val]
(cond-> v, val (conj val)))
to do this. Is there a specific operator or a more concise implementation?
I like the
cond->version and often use that to avoid repetition in theifversion. Don't forget to be explicit aboutfalsevalues, though. I also like to use Plumatic Schema to be explicit about the data shape entering and leaving the function:You may also be interested in another version using my favorite library:
For more complicated forms, using the placeholder symbol
itmakes it explicit where the value is being threaded. I also like the convenience functionsnot-nil?andappendsince they also make the intent of the code plainer.