Fork me on GitHub
#clojure
<
2022-01-09
>
pinkfrog03:01:22

I am logging some map, how can I enable pretty print on the format?

pinkfrog03:01:05

I’d like to perform in some nonintrusive way, such that log/info(:data data) will make the data to be formatted by default.

Cora (she/her)04:01:05

(with-out-str (clojure.pprint/pprint {:x 1 :y -1}))
;; => "{:x 1, :y -1}\n"

pinkfrog07:01:02

It’s more like I am going to switch the internal print function of clojure.tools.logging to pprint.

Eugen20:01:25

is there any smart way of not adding a map key if the value is nil ? I'm parsing an XML with riveted and I have things like:

(defn entity-relation-keymap->map
  [^Navigator e]
  {:field-name (vtd/attr e :field-name)
   :rel-field-name (vtd/attr e :rel-field-name)})
:rel-field-name is very often nil so I would like to NOT add it in the first place. Is there something like (when-let [elem ] add elem to map ? I'm thinking something like reader conditional / macro ?! Right now I use, which works - but maybe I can avoid adding them in the first place ?!
(defn clean-nils [m]
  (into {} (filter (comp some? val) m)))  

p-himik20:01:43

If it's just a few predefined keys, I'd use let + cond-> + assoc:

(let [b (get-b)]
  (cond-> {:a (get-a)}
    b (assoc :b b)))

p-himik20:01:28

https://github.com/weavejester/medley/ also has filter-vals, but I'd use that library only if you already depend on it.