Fork me on GitHub
#specter
<
2016-09-19
>
aaelony04:09:46

what is the idiomatic way in Specter to do the following:

(mapv (fn [x] {:name x}) (->> [[:a :b][:b :c][:c :d][:c :e]] flatten set vec ))
=> [{:name :e} {:name :c} {:name :b} {:name :d} {:name :a}]

aaelony04:09:51

taking a vector and constructing a map with new keys and vals?

dev-hartmann10:09:25

i am trying to add to a list inside a map of maps if the if the id matches. my code looks like that: (def same-id? (specter/comp-paths (specter/paramsfn [id-key id] [item] (= id (get item id-key)))))

dev-hartmann10:09:40

(specter/select [specter/ALL same-id? (:entity-id entity) (:entity-val entity)] db)

dev-hartmann10:09:30

i know the actual transform is missing, but i have a feeling i missunderstood how to use specter

nathanmarz10:09:26

can you give an example of input/output?

nathanmarz10:09:48

@aaelony that's an aggregation producing a brand new data structure construction, so Specter won't be any better than transducers for that case

nathanmarz10:09:33

@aaelony but this is an efficient way to do it:

(->> [[:a :b][:b :c][:c :d][:c :e]]
     (traverse [ALL ALL])
     (into #{})
     (into [] (map (fn [k] {:name k}))))

dev-hartmann10:09:40

entity = {:entity-id :advisory-id :entity-val :"2016-1425"} db = {:advisories {:list {:advisory-id "2016-1623" :cvss {:c 1 :d 2 :e 3} :tags ({:tag-key "test-tag" :tag-value nil} {:tag-key "test-tag-2" :tag-value 12})}}}

dev-hartmann10:09:11

at the moment i don't get any output because i cannot locate paramsfn

dev-hartmann10:09:41

what i want to do is, append a tag to tags inside advisory inside the advisories list if the id matches

nathanmarz11:09:38

@dev-hartmann looking for expected input/output

nathanmarz11:09:09

paramsfn was removed in 0.13.0 because it's no longer necessary

dev-hartmann11:09:38

expected output is the updated db map

dev-hartmann11:09:31

input is entity map like above and a tag {:tag-key "something" :tag-value "optional"}

nathanmarz11:09:57

I don't know what you mean, so I need a specific minimal example of input/output in order to help you

dev-hartmann11:09:54

(transform [(filterer #( = (:entity-val entity) (:entity-id %))) ALL] #(conj (:tags %) tag) db)]

dev-hartmann11:09:10

that's what i am trying at the moment, with the data from above

dev-hartmann11:09:04

as a result i get the unchanged map

nathanmarz11:09:58

@dev-hartmann a specific minimal example is something like [1 2 3] => [2 3 4]

dev-hartmann11:09:00

ah ok, sry, did not understand -.-

dev-hartmann11:09:11

{:entity-id :item-id :entity-val 1} 
{:tag-key "test2" :tag-value "2"}
{:items {:item-id 1 :sub-items ({:tag-key :tag "value"} {:tag-key "tag2" "value2"} {:tag-key "tag3" "value1"})}
        {:item-id 2 :sub-items ({:tag-key "tag1" "value"})}} 
->
{:items {:item-id 1 :sub-items ({:tag-key :tag "value"} {:tag-key :tag2 "value2"} {:tag-key :tag3 "value1"})}
        {:item-id 2 :sub-items ({:tag-key "tag-test" "value"} {:tag-key "test2" :tag-value "2"})}} 

nathanmarz11:09:10

do you mean for value of :items to be in a list of some sort?

nathanmarz11:09:06

and here you have the append going to subitems of item id 2, but your input says entity-val 1

dev-hartmann11:09:45

my brain is full on monday, sry again, entity-val should be 2

nathanmarz11:09:34

one way to do it: (setval [:items ALL #(= (get % (:entity-id entity)) (:entity-val entity)) :sub-items END] [tag] db)

dev-hartmann11:09:39

thank you very much, seeing your answer explains how i got the way the path is created all wrong. thanks again

nathanmarz11:09:31

I would also suggest using vectors instead of lists for sub-items, as appending to a vector is much more efficient

dev-hartmann11:09:27

that makes sense, thanks for the advice