Skip to content

Instantly share code, notes, and snippets.

@rotaliator
Created September 22, 2022 07:22
Show Gist options
  • Save rotaliator/499206c151561f9eae66e11abb04f3b4 to your computer and use it in GitHub Desktop.
Save rotaliator/499206c151561f9eae66e11abb04f3b4 to your computer and use it in GitHub Desktop.
Convert list of map to data table
(defn maps-to-table
([labels list-of-maps]
(maps-to-table labels list-of-maps true))
([labels list-of-maps with-headers?]
(let [header (when with-headers? [(mapv name labels)])
data (mapv (apply juxt labels) list-of-maps)]
(if with-headers?
(concat header data)
data))))
(comment
(maps-to-table [:col1 :col2]
[{:col1 "val1-col1" :col2 "val1-col2" :unused "not-relevant"}
{:col1 "val2-col1" :col2 "val2-col2"}])
;; => (["col1" "col2"]
;; ["val1-col1" "val1-col2"]
;; ["val2-col1" "val2-col2"])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment