Last active
December 12, 2017 11:13
-
-
Save krisleech/54d58b722fd294576e51f7514b51567f to your computer and use it in GitHub Desktop.
Clojure map->vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def data | |
'({:comments "asd" | |
:expenditure 123.00M | |
:id 1 | |
:ukcrc_category "Congenital Disorders," | |
:year 2017} | |
{:comments nil | |
:expenditure 12.00M | |
:id 2 | |
:ukcrc_category "Cardiovascular" | |
:year 2017})) | |
;; convert to collection, however we only want some keys... | |
(map vals data) ;; ((1 2017 "Congenital Disorders," 123.00M "asd") (2 2017 "Cardiovascular" 12.00M nil)) | |
;; use first record to find out how... | |
(def rec (first data)) ;; {:comments "asd" :expenditure 123.00M :id 1 :ukcrc_category "Congenital Disorders," :year 2017} | |
;; works, just the id and comment keys... | |
(into [] [(:id rec) (:comments rec)]) ;; [1 "asd"] | |
;; use on whole collection... | |
(map #(into [] [(:ukcrc_category %) (:expenditure %)]) data) | |
;; or an alternative | |
(map (juxt :ukcrc_category :expenditure) data)` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment