specter

Latest version: 1.1.3
spieden 2018-03-30T22:03:33.000098Z

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

nathanmarz 2018-03-30T22:20:58.000082Z

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

nathanmarz 2018-03-30T22:22:02.000192Z

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

spieden 2018-03-30T22:41:22.000068Z

ahh

spieden 2018-03-30T22:41:34.000048Z

ok thanks! @nathanmarz

spieden 2018-03-30T22:49:15.000265Z

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}