Fork me on GitHub
#specter
<
2017-03-24
>
snacks02:03:11

hi: If i have a nested map like this map1 {:a [{:b 1 c:3} {:b 2 c:0}] :b 0 } I need to update :b key based on the :c from one of these nested maps under :a and I have a filter function fn1 to select it based on :c value . is this the right way to do it?

(transform [(collect :a ALL (selected? [:c ?fn1]) :b) :b] (fn [new old] new) map1)

nathanmarz03:03:33

the code isn't matching up with your description

nathanmarz03:03:58

the code is collecting :b values, not :c values

nathanmarz03:03:59

you're also collecting a sequence of values, not "one of" the values

snacks03:03:11

yes it is collecting :b value, but using :c values to select which map to collect :b values from. so if fn1 is ??even it will return [1] while ?odd will return [2].

nathanmarz03:03:38

I think you have that reversed

nathanmarz03:03:52

if fn1 is odd? it will collect [1]

nathanmarz03:03:30

but yea, the code looks correct as long as you're looking to set :b in the top-level map to a sequence

nathanmarz03:03:46

also you don't need the [...] in selected?

nathanmarz03:03:56

(selected? :c fn1)

snacks03:03:11

you can use setval with collect?

nathanmarz03:03:54

I mean, it will execute but collected values will be ignored

borkdude13:03:41

How do I prune a map in specter? select only selects the values, but I want something like a recursive select-keys. I can’t find a good example of this on the wiki

nathanmarz13:03:18

@borkdude you can do something like this:

(def data {:a {:a 1 :b 2 :c 3} :b {:a 1 :b 2 :c 3 :d 4} :c {:a 1}})

(def ALL-MAPS
  (recursive-path [] p
    (if-path map?
      (stay-then-continue
        MAP-VALS p)
      )))

(transform ALL-MAPS #(select-keys % [:a :b]) data)
;; => {:a {:a 1, :b 2}, :b {:a 1, :b 2}}