Fork me on GitHub
#specter
<
2016-09-17
>
karan21:09:44

hey guys, is there an idiomatic way to select a slice of a nested map given a predicate for some level? for example, using even?, {:a {:b 1, :c 2}, :d {:e 3, :f 4}, :g {:h 5}} -> {:a {:c 2} :d {:f 4}}

karan21:09:07

so far I have something like this, which is pretty close:

(transform [MAP-VALS]
    #(into {} 
               (select-one (filterer LAST even?) %))
      ex)

karan21:09:40

with (def ex {:a {:b 1, :c 2}, :d {:e 3, :f 4}, :g {:h 5}})

karan21:09:38

the (into {} … ) doesn’t seem like the best solution. maybe if there were a type-aware select? seems to be an issue from a while back here: https://github.com/nathanmarz/specter/issues/127

nathanmarz21:09:30

@karan sounds like that would be a variant on submap that takes in a path to evaluate the vals, e.g. (filtered-submap even?) or (filtered-submap (filterer even?) (view count) #(>= % 2))

nathanmarz21:09:37

see selected? implementation for an example of a navigator which uses a path

karan21:09:39

@nathanmarz thanks, I’ll try that!