简体   繁体   中英

Group-by and data get

I think I am misunderstanding GET in clojure - I am trying to mould 1 data set from another, from the below A to B.

A
ID REGION MIN
10346 GLBL 106
10346 ASPAC 106
10346 NA 106
10346 LATAM 106
10346 EMEA 106
10347 GLBL 32
10347 ASPAC 32
10347 NA 32
10347 LATAM 32
10347 EMEA 32
10349 NA 10
10327 NA
10344 EMEA 8
10342 ASPAC 292
10342 EMEA 292
10348 ASPAC 15
10422 EMEA 37
10438 NA 0

B
ID EMEA NA ASPAC GLBL LATAM
10346 106 106 106 106 106 10347 32 32 32 32 32
10349 0 10 0 0 0
10327 0 0 0 0 0
10344 8 0 0 0 0
10342 292 0 292 0 0
10348 0 0 15 0 0
10422 37 0 0 0 0
10438 0 0 0 0 0

The group by is working but I am getting null values for all the regions, I though filtering on the region I could use get to obtain that value for MIN in that record and map it to the new region field - any advise on what I am doing wrong here? Or what I should be using instead of GET?

(defn- create-summary [data]
(->> data

     (group-by :ID
     vals
     (map 
        (fn [recs]
            (let [a (fn [priority](get :MIN (filter #(= priority (:REGION %)) recs)))]
                {:ID (:ID (first recs))
                 :EMEA (a "EMEA")
                 :NA (a "NA")
                 :GLBL (a "GLBL") 
                 :LATAM (a "LATAM")
                 :ASPAC (a "ASPAC")
                 })))

    ))

This:

(let [a (fn [priority](get :MIN (filter #(= priority (:REGION %)) recs)))]

Should be

(let [a (fn [priority](get (first (filter #(= priority (:REGION %)) recs)) :MIN))]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM