Fork me on GitHub
#specter
<
2021-05-03
>
richiardiandrea18:05:37

Hi folks again 😄 How would I go if I wanted to set a key in a map that is missing? For instance I only want to add :foo nil if it's not already there

richiardiandrea19:05:25

this is what I have come up so far:

(defn assoc-mode-nils
  "Add nil to empty modes."
  [root]
  ;; Please do not remove this or you will break spec and frontend.
  (sp/transform
   [maps/POST-ORDER-TREE-VALS
    (sp/must :descriptor)
    (sp/pred #(= (:type %) enum/segmod-element-type-heart-lv-subsegment))
    (sp/multi-path [(sp/keypath (keyword cmr.scoring/mode-function)) (sp/nil->val nil)]
                   [(sp/keypath (keyword cmr.scoring/mode-edema)) (sp/nil->val nil)])]
   identity
   root))

Jeff Evans21:05:15

(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:a nil :b 4})
=> {:a nil, :b 4}
(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:a 10 :b 4})
=> {:a 10, :b 4}
(s/setval [(s/cond-path (s/not-selected? (s/map-key :a)) (s/keypath :a))] 2 {:b 4})
=> {:b 4, :a 2}

Jeff Evans21:05:19

not sure if there’s a better way