Fork me on GitHub
#specter
<
2017-09-29
>
teawaterwire17:09:15

Hi there, I'm having trouble doing the following:

;; Considering this map m
(def m {:a 3
        :b {10 {:c 3}
            2 {:d 4}
            20 {:c 5}}})
;; I'd like to remove entries in the :b map if 
;; the key is < 15
;; and 
;; the value doesn't contain :d 
;; so it's like:
; {:a 3
;  :b {2 {:d 4}
;      20 {:c 5}}}

teawaterwire17:09:16

I managed to do it with (sp/setval [:b sp/ALL (fn [[k v]] (and (< k 15) (not (contains? v :d))))] sp/NONE m) but I guess there could be a more elegant solution?

nathanmarz18:09:25

here's a way that doesn't use any anonymous functions:

(setval
  [:b
   ALL
   (not-selected? LAST (must :d))
   FIRST
   (pred< 15)]
  NONE
  m)

teawaterwire18:09:37

Amazing, thanks! I tried something with not-selected? but I got stuck at:

(setval
 [:b
  (not-selected? MAP-VALS (must :d))
  MAP-KEYS
  (pred< 15)]
 NONE
 m)

nathanmarz19:09:22

that not-selected? call is a filter on the map as a whole

nathanmarz19:09:49

using ALL lets you test both conditions on the same entry

teawaterwire19:09:47

Neat :thumbsup: