Fork me on GitHub
#specter
<
2018-03-30
>
spieden22:03:33

trying to recursively remove map entries with nil values. here’s a start on the path:

(def nil-map-entries
  (spr/recursive-path [] p
    (spr/cond-path
      #(and (instance? MapEntry %)
            (nil? (val %)))
      spr/STAY

      #(coll? %)
      [spr/ALL p])))
although i can select them with this, transforming doesn’t seem to match on them:
(spr/select [nil-map-entries] {:foo nil :bar :bam})
=> [[:foo nil]]
(spr/transform [nil-map-entries] (constantly spr/NONE) {:foo nil :bar :bam})
=> {:foo nil, :bar :bam}
never experienced this kind of incongruity

nathanmarz22:03:58

@spieden specter navigates to vector for map entries on transform, not MapEntry object

nathanmarz22:03:02

I suggest handling maps separately from other collections in your path, which will make navigation more explicit and remove the need to check the type

spieden22:03:15

here’s what i wound up with, for posterity:

(def nil-map-entries
  (spr/recursive-path [] p
    (spr/cond-path
      map?
      [spr/ALL (spr/if-path #(nil? (second %))
                            spr/STAY
                            p)]

      #(coll? %)
      [spr/ALL p])))
(spr/transform [nil-map-entries] (constantly spr/NONE) {:foo {:baz nil} :bar :bam})
=> {:foo {}, :bar :bam}