Convert map of vectors to vectors of columns in Clojure -


i have collection (or list or sequence or vector) of maps so:

{ :name "bob", :data [32 11 180] } { :name "joe", :data [ 4  8  30] } { :name "sue", :data [10  9  40] } 

i want create new vectors containing data in vector "columns" associated keys describe data, so:

{ :ages [32 4 10], :shoe-sizes [11 8 9], :weights [180 30 40] } 

actually, simple list of vectors might adequate, i.e.:

[32 4 10] [11 8 9] [180 30 40]   

if it's better/easier make original list vector, that's fine; whatever's simplest.

given

(def records   [{:name "bob" :data [32 11 180]}    {:name "joe" :data [ 4  8  30]}    {:name "sue" :data [10  9  40]}]) 

you next transformations desired result:

(->> records      (map :data) ; extract :data vectors       ; => ([32 11 180] [4 8 30] [10 9 40])      (apply map vector) ; transpose      ; => ([32 4 10] [11 8 9] [180 30 40])      (zipmap [:ages :shoe-sizes :weights])) ; make map       ; => {:weights [180 30 40], :shoe-sizes [11 8 9], :ages [32 4 10]} 

without comments looks little bit cleaner:

(->> records       (map :data)      (apply map vector)      (zipmap [:ages :shoe-sizes :weights])) 

without threading macro equivalent more verbose:

(let [extracted (map :data records)       transposed (apply map vector extracted)       result (zipmap [:ages :shoe-sizes :weights] transposed)]   result) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -